Link to home
Start Free TrialLog in
Avatar of zippy21
zippy21

asked on

Connecting To Access Database

I just want to connect to an Access Db in code, and then put the value(s) of a text box and/or listbox in a table called Table1 with a field called Field1.   All I need is the code, I've been using the Data Access Control, and I would prefer to learn how to use code.

Thanks,

Zippy21
Avatar of bobbit31
bobbit31
Flag of United States of America image

make sure to reference MS ActiveX Data Object (ADO) under projects/references:

Dim myConn as New ADODB.Connection
Dim myRS as New ADODB.Recordset

myConn.open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security " & _
          "Info=False;Data Source=<path to db>"
with myRS
   .Open "Select Field1 from Table1"
   .addNew
   .Fields("Field1") = Form1.text1.text '' your textbox
   .update
   .close
end with

set myRS = nothing
myConn.close
set myConn = nothing
Avatar of zippy21
zippy21

ASKER

I get the following error:

RunTime 3709
The connection cannot be used to perform this operation.  It is either closed or invalid in this context.

Here's the code I used:
Private Sub cmdSave_Click()
Dim myConn As New ADODB.Connection
Dim myRS As New ADODB.Recordset

myConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security " & _
         "Info=False;Data Source=" & App.Path & "\SysInfo.mdb"
With myRS
  .Open "Select * from tblComputerInfo"
  .AddNew
  .Fields("ComputerName") = Me.txtComputerName.Text
  .Update
  .Close
End With

Set myRS = Nothing
myConn.Close
Set myConn = Nothing
End Sub
in your open statement, you didn't select the connection.  Your connection isn't tied to your recordset.  

try this
.Open "Select * from tblComputerInfo", myConn
Avatar of zippy21

ASKER

It says my record source cannot be updated.   Why would that be?
You might also need to specify a cursortype and cursorlocation:

Private Sub cmdSave_Click()
Dim myConn As New ADODB.Connection
Dim myRS As New ADODB.Recordset

myConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security " & _
        "Info=False;Data Source=" & App.Path & "\SysInfo.mdb"
With myRS
 .CursorType = adOpenDynamic
 .CursorLocation = adUseClient
 .Open "Select * from tblComputerInfo", myConn
 .AddNew
 .Fields("ComputerName") = Me.txtComputerName.Text
 .Update
 .Close
End With

Set myRS = Nothing
myConn.Close
Set myConn = Nothing
End Sub
Avatar of zippy21

ASKER

I am still getting the same error....
Run-time error 3251
Current recordset does not support updating.  This may be a limitation of the provider, or of the selected locktype.
Avatar of Richie_Simonetti
Which version of Access mdb?
Avatar of zippy21

ASKER

2000
Avatar of zippy21

ASKER

2000
ASKER CERTIFIED SOLUTION
Avatar of mdougan
mdougan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeah, oops, should be something like:

myRS.open "Select Field1 from Table1", myConn, adOpenForwardOnly, adLockOptimistic
Also, I found ADO to be really picky.  Make sure your table has a primary key defined, and no extranious spaces or special characters in the table and field names.
bobbit: adOpenForwardOnly allows updates?
> adOpenForwardOnly allows updates?

it does for me
Did you get it working?