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
Thanks,
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.OL EDB.4.0;Pe rsist 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
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.OL
"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
try this
.Open "Select * from tblComputerInfo", myConn
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.OL EDB.4.0;Pe rsist 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
Private Sub cmdSave_Click()
Dim myConn As New ADODB.Connection
Dim myRS As New ADODB.Recordset
myConn.Open "Provider=Microsoft.Jet.OL
"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
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.
Run-time error 3251
Current recordset does not support updating. This may be a limitation of the provider, or of the selected locktype.
Which version of Access mdb?
ASKER
2000
ASKER
2000
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yeah, oops, should be something like:
myRS.open "Select Field1 from Table1", myConn, adOpenForwardOnly, adLockOptimistic
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
it does for me
Did you get it working?
Dim myConn as New ADODB.Connection
Dim myRS as New ADODB.Recordset
myConn.open "Provider=Microsoft.Jet.OL
"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