Hello,
I am trying to build a string programmatically to use as the rowsource property of a combobox.
I got the idea from this page: Persistent Caching Of Combo Boxes
The code that I put together almost works as I needed to work, having only a couple of issues. In one of the xml files that are saved from the recordset there is a value that contains double quotes that looks like this:
Code:
<z:row PublisherName='"A" Easy Street, LLC'/>
When the combobox is the values, this specific value gets truncated, and only an A is visible, no double quotes, and the rest of the text after the A is lost.
The other issue is that a second xml file there is a value that contains a comma inside parenthesis:
Code:
<z:row Category='9' ProductDescription='Easy Play (Piano, Guitar) or short PD'/>
When the value is put in the combobox, the text gets splitted into 2 columns like:
Code:
Column 1 Column 2
Easy Play (Piano Guitar) or short PD
The comma is removed and what should be one whole value, is now two.
I do have, in the code an if statement that checks if the string value contains either double quotes or commas, but for some reason even though when I add a watch or in the immediate window I see that the value looks as it should, when it gets passed to the combobox, they get truncated.
I am now going to include how I call the function, and the contents of the function. I hope someone can share a light here and help me solve this small couple of issues.
Function call:
Code:
cboBlockReason: combobox being passed
qstrcBlockReason: is the query
1: is the number of columns to use in the for iteration
"BlockingDescription": name of the xml file to be saved and then read from.
BlockReason.RowSource = CacheRecordSet(cboBlockReason, qstrcBlockReason, 1, "BlockingDescription")
Function contents:
Code:
Function CacheRecordSet(pCombo As ComboBox, _
pQuery As String, _
pCols As Variant, _
pDataSource As String) As Variant
Dim s As String
Dim c As Integer
Dim rs As ADODB.Recordset
If Dir(CacheDir, vbDirectory) = "" Then
Shell ("cmd /c mkdir """ & CacheDir & """")
End If
On Error Resume Next
Kill CacheDir & pDataSource & FileType
Set rs = New ADODB.Recordset
With rs
.ActiveConnection = CurrentProject.Connection
.Open pQuery
.Save CacheDir & pDataSource & FileType, adPersistXML
End With
Do Until rs.EOF
For c = 0 To pCols
If InStr(rs(c), """") > 0 _
Or InStr(rs(c), ",") > 0 Then
rs(c) = "'" & rs(c) & "'"
End If
s = s & rs(c) & ";"
Next c
rs.MoveNext
pCombo.AddItem (s)
Loop
' Now return the string as the RowSource to be used
CacheRecordSet = pCombo.RowSource
eofit:
On Error Resume Next
rs.Close
Exit Function
errhandler:
z = ErrorFunction(Err, Err.Description, Erl, "CacheRecordSet", , True)
Err = 0
Select Case z
Case 0: Resume Next
Case 1: GoTo eofit
End Select
End Function