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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

userform database

Status
Not open for further replies.

simoncpage

Programmer
Apr 4, 2002
256
GB
I am trying to connection information from a userform to an access database. I have tried using the following code but I keep getting an error that it cannot open the database? I am sure I have missed something trivial can anyone help?

any help or examples that anyone knows of this type would be great! :)

Simon
----------------------------------------
Sub ADOFrmSetup()

Dim cn As New ADODB.Connection
Dim Rs As New ADODB.Recordset

cn.ConnectionString = "c:\temp\db1.mdb"
cn.Open
Rs.Open "SELECT * FROM ADDRESSES", cn, _ CursorType:=adOpenStatic, _
LockType:=adLockReadOnly

Frm.Textbox1.value = Rs.Fields("First name")Rs.Close
cn.Close
Set RS = Nothing
Frm.Show
End Sub

 
have you reference the ADO object in tools /references ?

also migth be easie rto use DAO , reference the Mcirosoft 3.5 DAO libary and change yoru code to the following

hers teh example from the help file

Sub OpenDatabaseX()

Dim wrkJet As Workspace
Dim dbsNorthwind As Database
Dim dbsPubs As Database
Dim dbsPubs2 As Database
Dim dbsLoop As Database
Dim prpLoop As Property

' Create Microsoft Jet Workspace object.
Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)

' Open Database object from saved Microsoft Jet database
' for exclusive use.
MsgBox "Opening Northwind..."
Set dbsNorthwind = wrkJet.OpenDatabase("Northwind.mdb", _

True)

' Open read-only Database object based on information in
' the connect string.
MsgBox "Opening pubs..."
Set dbsPubs = wrkJet.OpenDatabase("Publishers", _
dbDriverNoPrompt, True, _
"ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers")

' Open read-only Database object by entering only the
' missing information in the ODBC Driver Manager dialog
' box.
MsgBox "Opening second copy of pubs..."
Set dbsPubs2 = wrkJet.OpenDatabase("Publishers", _

dbDriverCompleteRequired, True, _
"ODBC;DATABASE=pubs;DSN=Publishers;")

' Enumerate the Databases collection.
For Each dbsLoop In wrkJet.Databases
Debug.Print "Database properties for " & _
dbsLoop.Name & ":"

On Error Resume Next
' Enumerate the Properties collection of each Database
' object.
For Each prpLoop In dbsLoop.Properties
If prpLoop.Name = "Connection" Then
' Property actually returns a Connection object.

Debug.Print " Connection[.Name] = " & _
dbsLoop.Connection.Name
Else
Debug.Print " " & prpLoop.Name & " = " & _
prpLoop
End If
Next prpLoop
On Error GoTo 0

Next dbsLoop

dbsNorthwind.Close
dbsPubs.Close
dbsPubs2.Close
wrkJet.Close

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top