PC Review


Reply
Thread Tools Rate Thread

Connecting to Access database to populate a combobox.

 
 
dan dungan
Guest
Posts: n/a
 
      11th Mar 2010
Hi

Using Excel 2000 and Access 2000, the code below was working to
populate a combobox on a spreadsheet.

I'm attempting to use it to populate a combobox--cboQpn--on Userform4.

I want the user to type a competitor part number in textbox--
txtCompNum.

Then the macro will use that value to find our part number in the
access table and put the result in the combobox. I'm using a combobox
because there may be more than one record returned.

The code fails on this line: Dim wspDefault As Workspace with the
error, "compile error: User-defined type not defined"

Thanks for your feedback,

Dan

Sub CreateRecordSet()
On Error GoTo CreateRecordSetErrorHandler
Dim oldDbName As String
Dim wspDefault As Workspace
Dim dbsEAIQuote As Database
Dim strSQL As String
Dim strCompetitorPart As String
Dim strEAIPart As String
Dim rstFromQuery As Recordset

strCompetitorPart = UserForm4.txtCompNum.text
strEAIPart = UserForm4.cboQpn.text


'Set the path to the database
oldDbName = "K:/Customer Service/Quote/Database/EAIQuote_be.mdb"

'Create a default workspace Object
Set wspDefault = DBEngine.Workspaces(0)

'Create a Database object
Set dbsEAIQuote = wspDefault.OpenDatabase(oldDbName)

'The SQL statement
strSQL = "SELECT tblCrossNoDash.Scrubbed, " & _
"tblCrossNoDash.EAIPartNumber FROM tblCrossNoDash " & _
"WHERE (tblCrossNoDash.Scrubbed= '" & strCompetitorPart & "')"

'Create a Snapshot Type Recordset from the SQL query
Set _
rstFromQuery = dbsEAIQuote.OpenRecordset(strSQL, dbOpenSnapshot)

'load up combobox

'Show the number of fields returned
'MsgBox "there are " & rstFromQuery.Fields.Count & _
'" fields that were returned"

'Move to the last record in the recordset
' rstFromQuery.MoveLast

'Put the EAI part number in Combobox2

Do While Not rstFromQuery.EOF
UserForm4.cboQpn.AddItem rstFromQuery(1).Value
rstFromQuery.MoveNext
Loop

' Sheet6.ComboBox2.DropDown = rstFromQuery!EAIPartNumber
'Show the number of records returned
' MsgBox "there are " & rstFromQuery.RecordCount & _
' " records that were returned"
Exit Sub
CreateRecordSetErrorHandler:

End Sub
 
Reply With Quote
 
 
 
 
dan dungan
Guest
Posts: n/a
 
      11th Mar 2010
Ok. I hadn't selected the proper reference--Microsoft DAO 3.6 object
library.

Also I had mispelled the table name in the sql statement.

So now the code I'm using (shown below) doesn't populate the combobox.

I know sql part is working becuase I typed my varialbe in the
Immediate window and copied the sql to the access database and it
returned the proper data.

However the recordset is never created with this line:
Set rstFromQuery = dbsEAIQuote.OpenRecordset(strSQL, dbOpenSnapshot)

and the combobox is not populated.

Here's the code I'm using.

Does anyone have a suggestion?

Thanks,

Dan

Sub CreateRecordSet()
On Error GoTo CreateRecordSetErrorHandler
Dim oldDbName As String
Dim wspDefault As Workspace
Dim dbsEAIQuote As Database
Dim strSQL As String
Dim strCompetitorPart As String
Dim strEAIPart As String
Dim rstFromQuery As Recordset

strCompetitorPart = UserForm4.txtCompNum.text
strEAIPart = UserForm4.cboQpn.text


'Set the path to the database
oldDbName = "K:/Customer Service/Quote/Database/EAIQuote_be.mdb"

'Create a default workspace Object
Set wspDefault = DBEngine.Workspaces(0)

'Create a Database object
Set dbsEAIQuote = wspDefault.OpenDatabase(oldDbName)

'The SQL statement
strSQL = "SELECT tblCompetitorScrubbed.EAIPartNumber " & _
"FROM tblCompetitorScrubbed " & _
"WHERE (tblCompetitorScrubbed.CompetitorNumber= '" &
strCompetitorPart & "')"

'Create a Snapshot Type Recordset from the SQL query
Set rstFromQuery = dbsEAIQuote.OpenRecordset(strSQL,
dbOpenSnapshot)

'load up combobox

'Show the number of fields returned
MsgBox "there are " & rstFromQuery.Fields.Count & _
" fields that were returned"

'Move to the last record in the recordset
rstFromQuery.MoveLast

'Put the EAI part number in Combobox2

Do While Not rstFromQuery.EOF
UserForm4.cboQpn.AddItem rstFromQuery(1).Value
rstFromQuery.MoveNext
Loop

' Sheet6.ComboBox2.DropDown = rstFromQuery!EAIPartNumber
'Show the number of records returned
' MsgBox "there are " & rstFromQuery.RecordCount & _
' " records that were returned"
Exit Sub
CreateRecordSetErrorHandler:

End Sub
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Access 2003... in ComboBox query doesnt populate right.. Ahmed Microsoft Access Getting Started 3 22nd Nov 2007 07:26 PM
how to get Excel 07 combobox to populate from MS Access table? xz Microsoft Excel Misc 0 20th Nov 2007 05:45 PM
HELP: Need macro for excel to get data from server database and use results to populate an Access Database Clinton M James Microsoft Access 1 7th Oct 2007 04:32 PM
Populate ComboBox list with Access data =?Utf-8?B?SVRfcm9vZmVy?= Microsoft Excel Programming 10 11th Apr 2007 11:31 PM
populate comboBox from Database Jassim Rahma Microsoft C# .NET 1 31st Jan 2005 04:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:09 PM.