|
-
Sep 15th, 2005, 08:05 AM
#1
Thread Starter
New Member
ADO and comboboxes
I am working on a Visual Basic program using adodc to write and read from an Access database.
I have the following code:
Private Sub Form_Load()
With Adodc1
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\DataBase.mdb;Persist Security Info=False"
.RecordSource = "select FName from Table1 order by Fname"
End With
End Sub
I want to fill the list of a combobox with the names that the query returns. Please help!
Thanks in advance!
-
Sep 15th, 2005, 08:27 AM
#2
Re: ADO and comboboxes
Welcome to the forums. 
It has been years beyond count since I've used bound controls, so this is something of a shot in the dark, but combo boxes have a datasource property and I think you need to bind that to your data control that does the query.
-
Sep 15th, 2005, 09:07 AM
#3
Thread Starter
New Member
Re: ADO and comboboxes
Nice to meet you.
Yes, I know that there is a datasource property but I don't know the way to bind it...
-
Sep 15th, 2005, 10:44 AM
#4
Re: ADO and comboboxes
You actually want rowsource rather than datasource. Since you've got a reference to ADODC1 in your code I assume you've put a ADO Data Controller on your form and called it ADODC1. Select your datacombo and click on it's row source property, which should turn into a dropdown. Drop it down and you should see that your ADO data control is in the list, select it and you're done.
You'll probably have to call ADODC1.refresh to get the data control to pick up the data though.
BTW the datasource property is basically the same except it filters by the value in the bindid column and is used for showing information from child tables etc.
-
Sep 15th, 2005, 11:09 AM
#5
Re: ADO and comboboxes
 Originally Posted by FunkyDexter
You actually want rowsource rather than datasource. Since you've got a reference to ADODC1 in your code I assume you've put a ADO Data Controller on your form and called it ADODC1. Select your datacombo and click on it's row source property, which should turn into a dropdown. Drop it down and you should see that your ADO data control is in the list, select it and you're done.
You'll probably have to call ADODC1.refresh to get the data control to pick up the data though.
BTW the datasource property is basically the same except it filters by the value in the bindid column and is used for showing information from child tables etc.
I don't think he is using a datacombo, but rather a regular, standard VB combo. Will this still work with that control?
-
Sep 15th, 2005, 01:00 PM
#6
Thread Starter
New Member
Re: ADO and comboboxes
Yes I use the standard VB combo and it doesn't work...
I think that the solution is through code but I don't know the code I have to write...
Maybe using a loopin additem way? Really I don't know...
-
Sep 15th, 2005, 01:17 PM
#7
Re: ADO and comboboxes
Now writing code to do this is something I know how to do.
VB Code:
Dim ADOCn As ADODB.Connection
Dim ConnString As String
Dim adoRS As ADODB.Recordset
Dim sSQL As String
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\DataBase.mdb;Persist Security Info=False"
Set ADOCn = New ADODB.Connection
ADOCn.ConnectionString = ConnString
ADOCn.Open ConnString
Set adoRS = New ADODB.Recordset
sSQL = "select FName from Table1 order by Fname"
adoRS.Open sSQL, ADOCn
Do Until adoRS.EOF
Combo1.AddItem adoRS.Fields.Item("fname").Value
adoRS.MoveNext
Loop
adoRS.Close
ADOCn.Close
Set ADOCn = Nothing
Set adoRS = Nothing
-
Sep 16th, 2005, 05:06 AM
#8
Re: ADO and comboboxes
I don't think he is using a datacombo, but rather a regular, standard VB combo. Will this still work with that control?
Ooops, my mistake . No I don't think it'll work with a standard combo. I think you can bind a standard combo but I've always used code instead - much more control.
-
Sep 16th, 2005, 05:32 AM
#9
Thread Starter
New Member
Re: ADO and comboboxes
Well done! It worked! Thanks a lot guys!
-
Sep 16th, 2005, 06:11 AM
#10
Re: ADO and comboboxes
 Originally Posted by FunkyDexter
Ooops, my mistake  . No I don't think it'll work with a standard combo. I think you can bind a standard combo but I've always used code instead - much more control.
No need to be embarrassed Funky. I don't use bound controls so I actually didn't know the answer to that and was curious as it could be asked by someone else at some point.
Thanks for the info.
 Originally Posted by Volrath1979
Well done! It worked! Thanks a lot guys!
Good deal!
-
Sep 16th, 2005, 02:47 PM
#11
Junior Member
Re: ADO and comboboxes
 Originally Posted by Hack
Now writing code to do this is something I know how to do.
VB Code:
Dim ADOCn As ADODB.Connection
Dim ConnString As String
Dim adoRS As ADODB.Recordset
Dim sSQL As String
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\DataBase.mdb;Persist Security Info=False"
Set ADOCn = New ADODB.Connection
ADOCn.ConnectionString = ConnString
ADOCn.Open ConnString
Set adoRS = New ADODB.Recordset
sSQL = "select FName from Table1 order by Fname"
adoRS.Open sSQL, ADOCn
Do Until adoRS.EOF
Combo1.AddItem adoRS.Fields.Item("fname").Value
adoRS.MoveNext
Loop
adoRS.Close
ADOCn.Close
Set ADOCn = Nothing
Set adoRS = Nothing
Will similar code work with excel?
-
Sep 17th, 2005, 05:12 AM
#12
Re: ADO and comboboxes
 Originally Posted by lesthan60
Will similar code work with excel?
How do you mean?
What do you have and what do you need done with it?
-
Sep 17th, 2005, 02:28 PM
#13
Junior Member
Re: ADO and comboboxes
The form I will be building will need to read and write data to an excel file.
Last edited by lesthan60; Sep 17th, 2005 at 02:34 PM.
Reason: more info needed
-
Sep 19th, 2005, 07:08 AM
#14
Junior Member
Re: ADO and comboboxes
 Originally Posted by lesthan60
The form I will be building will need to read and write data to an excel file.
So, will a similar code work with excel?
-
Sep 19th, 2005, 07:53 AM
#15
Re: ADO and comboboxes
You are not connecting to a database, but a Excel spreadsheet. The approach is completely different.
The first thing you would need to do is set a reference to the Microsoft Excel Object Library, and open your sheet up. Have you ever done that before?
-
Sep 19th, 2005, 08:25 AM
#16
Junior Member
-
Sep 19th, 2005, 08:29 AM
#17
Re: ADO and comboboxes
Ok. From the VB IDE, click on Project/References
Locate the Microsoft Excel Object Library (x.x) (where x.x is the version you have)
Click on the Checkbox, then click OK.
Now that you have a reference to Excel, you can open your worksheet.
VB Code:
Private Sub cmdOpenWorkSheet_Click()
Dim xls As Excel.Application
Dim xlBook As Excel.Workbook
Set xls = New Excel.Application
Set xlBook = xls.Workbooks.Open(App.Path & "\yourworksheetname.xls")
End Sub
-
Sep 19th, 2005, 09:43 AM
#18
Junior Member
Re: ADO and comboboxes
Thanks Hack, OK - I've located and clicked reference to Excel, I've created a form and I've pasted your code to that form and I've created a Excel file and added that name to your code.
When I click text1, readwritetest.xls then open readwritetest.xls (read only) cell A1 has not changed. After exiting VB readwritetest.xls remains as read only.
visual basic code:-------------------------------------------------------------------------------
Private Sub cmdOpenWorkSheet_Click()
Dim xls As Excel.Application
Dim xlBook As Excel.Workbook
Set xls = New Excel.Application
Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
xlBook.Sheets("sheets").Cells(1, 1) = Me.Text1
End Sub
Private Sub Text1_Click()
Call cmdOpenWorkSheet_Click
End Sub
Last edited by lesthan60; Sep 19th, 2005 at 11:27 AM.
-
Sep 19th, 2005, 09:59 AM
#19
Re: ADO and comboboxes
Cool.
If you have questions on the reading and writing part, I would suggest you create a new thread in either Office Development or ClassicVB in which you could post your specific questions about your Excel Code.
This is, after all, Volrath1979's thread and dealth with ADO and Combo boxes. We are going off in a whole new direction here, and I think you need to start your own thread with your own specific questions.
-
Sep 19th, 2005, 11:31 AM
#20
Junior Member
Re: ADO and comboboxes
Yes, I'll start a new thread in Office Development. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|