I attended the C # training a few days ago and found that the format parameter replacement function is really practical, especially when writing SQL statements, such as an SQL statement.
Insert into (F1, F2, F3) values (V1, V2, V3)
If you want to use VB to write, it is hard to write and ugly to add a string of "" and. Basically, as long as there are more than one parameter, an error is required in almost 100%. If C # is used for writing, this is the case.
Str SQL = "insert into (F1, F2, F3) values ('{0}', '{1}', '{2 }')"
SQL = SQL. String. Format (SQL, V1, V2, V3)
The syntax may not be correct. It takes only a few days to train and you are not familiar with the C # syntax. This is easy to write, and the location of each parameter is clear at a glance. After thinking about it today, I finally decided to write a VB Function by myself. It took more than an hour to finally solve the problem. I added various error checks and it should be okay.
VB can only follow up to 25 rows, so it should be enough to write so much. If it is not enough, just add a few line breaks and parameters.
- Public Function rformat (STR as string ,_
- Byval V0 as variant ,_
- Optional byval V1 as variant = "{e }",_
- Optional byval v2 as variant = "{e }",_
- Optional byval V3 as variant = "{e }",_
- Optional byval V4 as variant = "{e }",_
- Optional byval V5 as variant = "{e }",_
- Optional byval V6 as variant = "{e }",_
- Optional byval V7 as variant = "{e }",_
- Optional byval V8 as variant = "{e }",_
- Optional byval V9 as variant = "{e }",_
- Optional byval V10 as variant = "{e }",_
- Optional byval V11 as variant = "{e }",_
- Optional byval V12 as variant = "{e }",_
- Optional byval v13 as variant = "{e }",_
- Optional byval V14 as variant = "{e }",_
- Optional byval V15 as variant = "{e }",_
- Optional byval V16 as variant = "{e }",_
- Optional byval v17 as variant = "{e }",_
- Optional byval V18 as variant = "{e }",_
- Optional byval V19 as variant = "{e }",_
- Optional byval V20 as variant = "{e }",_
- Optional byval V21 as variant = "{e }",_
- Optional byval v22 as variant = "{e }"_
- )
- Dim ret, I as integer, N as integer, ocerr as Boolean
- Dim islast as Boolean, CI as integer, P1, Plen
- ''Check parameters
- Islast = false
- For I = 0 to 22
- P1 = instr (STR, "{" & I &"}")
- Plen = Len ("{" & I &"}")
- If P1> 0 then
- Ci = Val (mid (STR, P1 + 1, Plen-2 ))
- If Ci <> I then
- Err. Raise 1, "rformat", "The provided parameterized string order does not match !! "
- End if
- If islast then
- Err. Raise 2, "rformat", "provided parameterized strings may be missing !! "
- End if
- N = I + 1
- Else
- Islast = true
- End if
- Next
- I = 0
- Ret = Str
- Ocerr = false
- If I> = N and v0 <> "{e}" then ocerr = true
- I = I + 1
- If I> = N and V1 <> "{e}" then ocerr = true
- I = I + 1
- If I> = N and V2 <> "{e}" then ocerr = true
- I = I + 1
- If I> = N and V3 <> "{e}" then ocerr = true
- I = I + 1
- If I> = N and V4 <> "{e}" then ocerr = true
- I = I + 1
- If ocerr then
- Err. Raise 1, "rformat", "too many parameters are provided! "
- End if
- I = 0
- Ret = Replace (Ret, "{" & I & "}", V0 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V1 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V2 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V3 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V4 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V5 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V6 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V7 & ""): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V8 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V9 & ""): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V10 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V11 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V12 & ""): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", v13 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V14 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V15 & ""): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V16 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", v17 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V18 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V19 & ""): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V20 & "): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", V21 & ""): I = I + 1
- Ret = Replace (Ret, "{" & I & "}", v22 & "): I = I + 1
- If instr (Ret, "{e}")> 0 then
- Err. Raise 1, "rformat", "Too few parameters are provided! "
- End if
- Rformat = RET
- End Function