Good day,
I am trying to create a query by form a have a slight problem when using the IN function. Selecting multiple parameters for the query automatically adds parenthesis to the text and I need to remove them as the IN function does not like them. The attached code produces the following result”
“(‘Wednesday’, ‘Thursday’, ‘Friday’)” while I only need (‘Wednesday’, ‘Thursday’, ‘Friday’).
I saw some example using Ltrim$ RTrim$ and Mid$ and are not sure how to implement it.
The code works fine as I do a multiple selection from a combo box and it is only this last step. Include in the code below is some code I copied supplied by Microsoft and the second (Function InParam(Fld, Param)) I do not think I need as acquire the information from my form.
Function GetToken(stLn, stDelim) maybe what I require but I am not sure how to link the two functions.
Thank you for your time.
Hennie
Dim declarations removed.
For Each varItm In ctl.ItemsSelected
For intI = 0 To ctl.ColumnCount - 1
Debug.Print ctl.Column(intI, varItm)
'Place "," after first item chosen
If Choice = Empty Then
Choice = Choice & "'" & ctl.Column(intI, varItm) & "'"
Else
Choice = Choice & ", '" & ctl.Column(intI, varItm) & "'"
Debug.Print Choice
End If
Next intI
Debug.Print
Next varItm
Microsoft's code
' The GetToken() function defines the delimiter character.
'============================================================
Function GetToken(stLn, stDelim)
Dim iDelim As Integer, stToken As String
iDelim = InStr(1, stLn, stDelim)
If (iDelim <> 0) Then
stToken = LTrim$(RTrim$(Mid$(stLn, 1, iDelim - 1)))
stLn = Mid$(stLn, iDelim + 1)
Else
stToken = LTrim$(RTrim$(Mid$(stLn, 1)))
stLn = ""
End If
GetToken = stToken
End Function
'============================================================
' The InParam() function is the heart of this article. When
' the query runs, this function causes a query parameter
' dialog box to appear so you can enter a list of values.
' The values you enter are interpreted as if you
' had entered them within the parentheses of the In() operator.
'============================================================
Function InParam(Fld, Param)
Dim stToken As String
'The following two lines are optional, making queries
'case-insensitive
Fld = UCase(Fld)
Param = UCase(Param)
If IsNull(Fld) Then Fld = ""
Do While (Len(Param) > 0)
stToken = GetToken(Param, ",")
If stToken = LTrim$(RTrim$(Fld)) Then
InParam = -1
Exit Function
Else
InParam = 0
End If
Loop
End Function
I am trying to create a query by form a have a slight problem when using the IN function. Selecting multiple parameters for the query automatically adds parenthesis to the text and I need to remove them as the IN function does not like them. The attached code produces the following result”
“(‘Wednesday’, ‘Thursday’, ‘Friday’)” while I only need (‘Wednesday’, ‘Thursday’, ‘Friday’).
I saw some example using Ltrim$ RTrim$ and Mid$ and are not sure how to implement it.
The code works fine as I do a multiple selection from a combo box and it is only this last step. Include in the code below is some code I copied supplied by Microsoft and the second (Function InParam(Fld, Param)) I do not think I need as acquire the information from my form.
Function GetToken(stLn, stDelim) maybe what I require but I am not sure how to link the two functions.
Thank you for your time.
Hennie
Dim declarations removed.
For Each varItm In ctl.ItemsSelected
For intI = 0 To ctl.ColumnCount - 1
Debug.Print ctl.Column(intI, varItm)
'Place "," after first item chosen
If Choice = Empty Then
Choice = Choice & "'" & ctl.Column(intI, varItm) & "'"
Else
Choice = Choice & ", '" & ctl.Column(intI, varItm) & "'"
Debug.Print Choice
End If
Next intI
Debug.Print
Next varItm
Microsoft's code
' The GetToken() function defines the delimiter character.
'============================================================
Function GetToken(stLn, stDelim)
Dim iDelim As Integer, stToken As String
iDelim = InStr(1, stLn, stDelim)
If (iDelim <> 0) Then
stToken = LTrim$(RTrim$(Mid$(stLn, 1, iDelim - 1)))
stLn = Mid$(stLn, iDelim + 1)
Else
stToken = LTrim$(RTrim$(Mid$(stLn, 1)))
stLn = ""
End If
GetToken = stToken
End Function
'============================================================
' The InParam() function is the heart of this article. When
' the query runs, this function causes a query parameter
' dialog box to appear so you can enter a list of values.
' The values you enter are interpreted as if you
' had entered them within the parentheses of the In() operator.
'============================================================
Function InParam(Fld, Param)
Dim stToken As String
'The following two lines are optional, making queries
'case-insensitive
Fld = UCase(Fld)
Param = UCase(Param)
If IsNull(Fld) Then Fld = ""
Do While (Len(Param) > 0)
stToken = GetToken(Param, ",")
If stToken = LTrim$(RTrim$(Fld)) Then
InParam = -1
Exit Function
Else
InParam = 0
End If
Loop
End Function