Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retreive from recordset

Status
Not open for further replies.

buddyel

MIS
Mar 3, 2002
279
US

I am using VBA in Excel and trying to retrieve a record from an access db and insert the fields for that record into corresponding cells.

Fields.item("dwgno") = Range("b8")

I would like the retrieve the record where the field (dwgno) matches the value entered in Cell B8 in the spreadsheet. I have trying for days, can anyone help....please. Thanks
 
Try the following:

Paste the following function into a new excel module.

Option Explicit

Function searchDB(SearchFor As String)

Dim strConnection As String
Dim objConn As Object
Dim myRS As Object
Dim mySQL As String


'Replace "c:\Your Path\database name.mdb" with the full path and name of your database

strConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = c:\Your Path\database name.mdb;" & _
"Jet OLEDB:Database Password=Scuba; "

Set objConn = CreateObject("ADODB.Connection")

objConn.Open strConnection

Set myRS = CreateObject("ADODB.Recordset")

'The following is the SQL uesd to search the table tablename where field1 = search string

mySQL = "SELECT tablename.fieldname1, tablename.fieldname2, tablename.fieldname3 " & _
"FROM tablename " & _
"WHERE tablename.fieldname1 ='" & SearchFor & "';"

Set myRS = objConn.Execute(mySQL)


If myRS.EOF Or myRS.BOF Then
MsgBox "Records not found"
Else
MsgBox "Records Found"
searchDB = myRS.fields("LastName")
End If

End Function


To use insert the following into a cell
"=searchDB(cellreference)"

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top