Forum Discussion
Set default value in ComboBox from a SQL query
- Mar 23, 2022
Okay, then, I was not treating the default as a string, which it needs to be.
Try this, using DLookup and the string delimiters.
Private Sub Form_Load()Dim strUserName As String
strUserName = DLookup("name", "users", "username = " & fncUser() )
Me.YourComboBoxNameGoesHere.DefaultValue = """" & strUserName & """"
End Sub
Ah, sorry I missed that you wanted different values than the username.
I was going to suggest a different approach, but I'm getting the same error you reported!
I have to do some additional testing to see what I'm missing.
Okay, then, I was not treating the default as a string, which it needs to be.
Try this, using DLookup and the string delimiters.
Private Sub Form_Load()
Dim strUserName As String
strUserName = DLookup("name", "users", "username = " & fncUser() )
Me.YourComboBoxNameGoesHere.DefaultValue = """" & strUserName & """"
End Sub
- markarelMar 23, 2022Brass Contributor
George Hepwor
If I put the user directly, the code works correctly, it seems that what is wrong is this part.
This works, but putting the user manually:
Maybe this will help to solve the problem...
- arnel_gpMar 27, 2022Iron Contributor
there is no need to Use Default value:
private sub form_load()
Me!nombre_caja = Dlookup("name", "yourQueryThatReturnsUserName")
end sub - George_HepworthMar 24, 2022Silver Contributor
I apologize for the delay in responding. This forum is uniquely bad at showing new posts on existing threads.
Yes, you need to qualify the call to the function with text delimiters, as shown here.
Private Sub Form_Load()
Dim strUserName As String
strUserName = DLookup("nombre", "fichaje", "usuario_registrado= '" & fncUser & "'")Me.nombre_caja.DefaultValue = """" & strUserName & """"
End Sub
- markarelMar 23, 2022Brass Contributor
Thank you very much for your solution, but I get the following erro executing the form:
Table name: fichaje
User name/lastname: nombre
Windows user: usuario_registrado
what could be wrong?