I created a custom excel form for collecting data. I connected it to Access to reset the data. However, I want to update Access every time the user clicks the submit button.
Basically, I need a Select statement to determine if id exists, and then if it doesn't exist, I need to use INSERT to add a new line. I am very new to any SQL, so any help would be great.
Here is the code that I have now, I need to adapt it to ADO.
Sub Update() Dim cnn As ADODB.Connection Dim MyConn Dim rst As ADODB.Recordset Dim StrSql As String Set cnn = New ADODB.Connection MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB With cnn .Provider = "Microsoft.ACE.OLEDB.12.0" .Open MyConn Set rst = New ADODB.Recordset rst.CursorLocation = adUseServer rst.Open Source:="Foam", ActiveConnection:=cnn, _ CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _ Options:=adCmdTable StrSql = "SELECT * FROM Foam WHERE FoamID = " & txtMyID Set rst = CurrentDb.OpenRecordset(StrSql, dbOpenDynaset) If (rst.RecordCount = 0) Then DoCmd.RunSQL "INSERT INTO Foam (ID, Part, Job, Emp, Weight, Oven) VALUES " & _ "(" & txtID & ", '" & txtField1 & "', '" & txtField2 & "', '" & txtField3 & "', '" & txtField4 & "', '" & txtField5 & "' );" End If ' Close the connection rst.Close cnn.Close Set rst = Nothing Set cnn = Nothing End With End Sub
source share