Using DAO to write to textbox in userform

D

dan dungan

Hi

I've used the code below to successfully update a textbox on a
worksheet.

When I tried to call this from a command button on a userform, it
failed with the error compile error: user defined type not defined

on the Dim wspDefault As Workspace line.

I'm grateful for any suggestions.

Thanks,

Dan

Sub CreateRecordSet()

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 = Sheet1.TextBox1.Text
strEAIPart = Sheet1.TextBox2.Text


'Set the path to the database
oldDbName = "C:/My Documents/Quote/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.CompetitorNumber, " & _
"tblCompetitorScrubbed.EAIPartNumber FROM
tblCompetitorScrubbed " & _
"WHERE (tblCompetitorScrubbed.CompetitorNumber= '" &
strCompetitorPart & "')"

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

'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 textbox2
Sheet1.TextBox2.Value = rstFromQuery!EAIPartNumber
'Show the number of records returned
MsgBox "there are " & rstFromQuery.RecordCount & _
" records that were returned"

End Sub
 
J

Joel

In VBA menu Tools - Referrences check the following box

Microsoft DAO 3.6 Object Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top