SQL Problem for updating Access Database from Excel

jcrone

New Member
Joined
Mar 15, 2016
Messages
15
I can not figure out what the issue is with my code. I can add to my database just fine but cannot update the database. when debugging once it hits the sql statement it jumps to the error handler. Here is the code

Code:
Private Sub Testbutton_Click()'Declaring the necessary variables.
Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rs As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath As String
Dim i As Integer
Dim x As Integer
'add error handling
On Error GoTo errHandler:


'get the path to the database
dbPath = Sheet2.Range("D23").Value


Set cnn = New ADODB.Connection ' Initialise the collection class variable


'Connection class is equipped with a —method— named Open
'—-4 aguments—- ConnectionString, UserID, Password, Options
'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath


Set rs = New ADODB.Recordset 'assign memory to the recordset
'Create the SQL statement to retrieve the data from table.
'This where I am having trouble 
rs.Open "SELECT * FROM GBLDB" & _
"WHERE ID = " & CLng(Me.txtRequest), ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
Options:=adCmdText


If rs.EOF And rs.BOF Then
'Close the recordet and the connection.
rs.Close
cnn.Close
'clear memory
Set rs = Nothing
Set cnn = Nothing
'Enable the screen.
Application.ScreenUpdating = True
'In case of an empty recordset display an error.
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
With rs
.Fields("Furn Pull").Value = cbfurniturepull.Value
.Fields("Date Time").Value = Date & " " & Time
.Fields("Building").Value = txtbuilding.Text
.Fields("Title").Value = txttitle.Text
.Fields("Contact").Value = txtname.Text & ": " & txtreqphone.Text
.Fields("Location/SPID").Value = spidNumber
.Fields("Scope").Value = txtJobDescrbtion.Text
.Fields("CB Date").Value = cbydate
.Update
End With






'Close the recordset and the connection.
rs.Close
cnn.Close
'clear memory
Set rs = Nothing
Set cnn = Nothing




On Error GoTo 0
Exit Sub


errHandler:
'clear memory
Set rs = Nothing
Set cnn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Import_Data"
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
There should be a space between the table name, GBLDB, and the WHERE clause.
 
Upvote 0
Now I am getting Error -2147217904 (No Value given for one or more required parameters.)
 
Upvote 0
I changed " WHERE ID = " to " WHERE [W0#] = " and seems to have fixed my issue

Thanks For help!
 
Upvote 0

Forum statistics

Threads
1,215,650
Messages
6,126,016
Members
449,280
Latest member
Miahr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top