how assign vba query to listbox (1 Viewer)

sohailcdc

Registered User.
Local time
Yesterday, 18:46
Joined
Sep 25, 2012
Messages
55
Hello there
I am total green in access specially in VBA world and trying to learn during my free time

I am able to learn to learn how to assign values to LISTBOX, based on already created Query (using Wizard)

Now I am trying to learn, HOW to create the query through VBA and assign to listbox instead of first creating query

What exactly I am trying to learn is upon opening of the my listbox show already existed records (for information)

Thanks in advance for your kind help

Following the information
tlb name = Currency
tlbfield1=Currencycode
tlbfield2=Currencyname

Private Sub Form_Load()
Dim sql1 As Integer
sql1 = SELECT Currency.[Currencycode] AS Code, Currency.[Currencyname] AS Name FROM [Currency];
DoCmd.RunSQL sql1
Me.listcurcode.RowSource = sql1
Me.listcurcode.Requery
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:46
Joined
Aug 30, 2003
Messages
36,139
You're close. Try

Dim sql1 As String
sql1 = "SELECT Currency.[Currencycode] AS Code, Currency.[Currencyname] AS Name FROM [Currency]"

Me.listcurcode.RowSource = sql1
 

MarvinM

Registered User.
Local time
Yesterday, 18:46
Joined
Nov 26, 2013
Messages
64
sohailcdc,

You've got it already!
Just take your sql1 string and put it into the Row Source property of your ListBox:
Code:
SELECT Currency.[Currencycode] AS Code, Currency.[Currencyname] AS Name FROM [Currency];

_________________
Regards,
Marvin M :cool:
Windows 7 Professional, MS Access 2007/2010
Windows 8 Professional, MS Access 2013
-------------------------------------------------------------------------------------------------------------------
If my post has helped you, please click the scales or click the 'Thumbs up'. Thanks!
-------------------------------------------------------------------------------------------------------------------
 

MarvinM

Registered User.
Local time
Yesterday, 18:46
Joined
Nov 26, 2013
Messages
64
Oh, I see what you mean. Paul is right.
I always put a semicolon at the end of that SQL statement. Does it work without it?
 

sohailcdc

Registered User.
Local time
Yesterday, 18:46
Joined
Sep 25, 2012
Messages
55
Thank you, it works perfectly Yahoo! I learn something today
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:46
Joined
Aug 30, 2003
Messages
36,139
Happy to help!

Marvin, the semicolon is optional. I never put them in.
 

Users who are viewing this thread

Top Bottom