populate combobox from SQL db
Hello, I have been working at this for days and am no better off. I could really use some help.
I need to populate a combobox with data from a column in an SQL table. I have put together the code below but am having an error at this line:
invBegNum.additem rs.Fields.Item("RMDNUMWK").Value
Error message:
"Object doesn't support this property or method"
I'm really a newb but am trying hard to get this. Here is the code. Please help.
VB Code:
Sub getInvData()
Dim conn As ADODB.Connection
Dim connString As String
conString = "Provider=sqloledb;Data Source=localhost;Initial Catalog=BBTN;User Id=scott;Password=bigdaddy"
Set conn = New ADODB.Connection
conn.ConnectionString = conString
conn.Open connString
Dim rs As ADODB.RecordSet
Dim sSQL As String
Set rs = New ADODB.RecordSet
sSQL = "select RMDNUMWK from SV00564 order by RMDNUMWK"
rs.Open sSQL, conn
Do Until rs.EOF
invBegNum.additem rs.Fields.Item("RMDNUMWK").Value
rs.MoveNext
Loop
rs.Close
conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
Added vbcode tags - Hack
Re: populate combobox from SQL db
The only thing that might possibly be wrong is that invBegNum is not the name of the Combobox control. I say this because additem is not in proper case, meaning intellisense could not find that property in that control.
Is invBegNum an ImageCombo?
Re: populate combobox from SQL db
gogetsome:
Just giving your code a quick glance it looks like you have an error here:
conn.ConnectionString = conString
conn.Open connString
In the first line you have conString and in the second line you have connString.
Make them both the same and see what happens.
I think it should work.
Hope this helps.
Re: populate combobox from SQL db
Thank you AIS4U for the fat finger catch.
brucevde the object is a combobox. I then tried to use a listbox and both receive the same error. Now, to show my newb. I'm trying to do this in VBA on a form in Great Plains. I thought that straight VB works but am unsure why the combobox does not accept .additem. Is there something else that could be causing this?
Re: populate combobox from SQL db
hi,
try this though your code seems to be all right
<vbcode>comboname.additem("" &rs.fields("field_name"))<\vbcode>
Re: populate combobox from SQL db
Thank you mohit_ranka for joining in this problem. I tried your suggestion of invbegnum.additem ("" &rs.Fields("rmdumwk")) but still receive the same message. This is crazy. When I hover over the errored out line I see the expected data so I know the recordset is being populated.
Re: populate combobox from SQL db
Post the Form where the problem occurs.
Re: populate combobox from SQL db
can you post the error message?
Re: populate combobox from SQL db
mohit_ranka the error reads:
run-time error '438':
Object doesn't support this porperty or method
brucevde how do I get the code to post the form? I know how to do this in VS2005 but not in microsoft visual basic 6.3 VBA.
Maybe this can help route out the problem:
When I write this line of code, intellisence does not list .additem as a method or propery for invbegnum. This method property does belong to the combobox.
Re: populate combobox from SQL db
This might be an issue with VBA not having the same functionality of a normal vb language (.net or Vb6). I'm not sure, but it sounds like that might be the case.
Edit: From what i've read around the forum it looks like the comboboxes in VBA don't have the additem property.
Edit: I would try using the rowsource property.
Re: populate combobox from SQL db
Quote:
Originally Posted by gogetsome
I'm trying to do this in VBA on a form in Great Plains.
VBA question moved to Office Development (although I have no clue what Great Plains is.)