Variable Name for Listbox

TheSearcher

Registered User.
Local time
Today, 18:08
Joined
Jul 21, 2011
Messages
356
I'm getting a type mismatch error in the following code on the "Set lstname = txt_CurrentListBox" line.
I realize that lstname is an object (listbox) and txt_CurrentListBox is a textbox holding a string so the error makes sense. However, what can I do to use a variable for the listbox name so that this can work? Does anyone know the correct syntax?
Your help is much appreciated!

Code:
Dim lstname As listbox
Dim varItem As Variant

Set lstname = txt_CurrentListBox
    For Each varItm In lstname.ItemsSelected
        sql1 = "Insert into tbl_Note_PT_Objective_1 (Note_ID, [Category], Options) "
        sql1 = sql1 & "select " & Globals.glb_NoteID & ", '" & txt_Category & "', '" & lstname.ItemData(varItm) & "'"
        DoCmd.SetWarnings False
        DoCmd.RunSQL sql1
        DoCmd.SetWarnings True
    Next varItm
 
Me.txt_CurrentListBox

And just glancing I see that sql1 has a SELECT clause but no FROM. That's not going to work.
 
If the tex box holds a name of the current listbox that is on the current form, then.
Set lstname = me.controls(txt_CurrentListBox)
 
So do a Values () instead of a Select for the insert.
 

Users who are viewing this thread

Back
Top Bottom