Hiya People,
I am trying to check if a record exists before an INSERT statement. Easy enough on the same database and I see a few answers already here, but what if you want to chek if the record exists in a seperate database?
I tried something like:
I am trying to check if a record exists before an INSERT statement. Easy enough on the same database and I see a few answers already here, but what if you want to chek if the record exists in a seperate database?
I tried something like:
Code:
Private Sub btn_transfer_Click()
On Error GoTo Err_Transfer
Dim ws As Workspace
Dim db As DAO.Database
Dim bInTrans As Boolean
Dim strSQL As String
Dim strMSG As String
Dim rsc As New ADODB.Recordset
Set rs = New ADODB.Recordset
rs.ActiveConnection = ????
' Initalise DB Object inside a transaction
Set ws = DBEngine(0)
ws.BeginTrans
bInTrans = True
Set db = ws(0)
' Check if record exists?
strSQL = "SELECT * FROM SAMNZ IN ""C:\Users\Owner\Desktop\DB\test.accdb"" WHERE (EventNo='" & EventNo & "');"
Debug.Print (strSQL)
rsc.Open strSQL
If rsc.EOF Then
' Execute the append
strSQL = "INSERT INTO SAMNZ IN 'C:\Users\Owner\Desktop\DB\test.accdb' SELECT * FROM table1 WHERE (IDNo='" & IDNo & "');"
Debug.Print (strSQL)
db.Execute strSQL, dbFailOnError
End If
Exit_DoTransfer:
' Cleaning Up
On Error Resume Next
Set db = Nothing
If bInTrans Then
ws.Rollback
End If
Set ws = Nothing
rsc.Close
Set rsc.ActiveConnection = Nothing
Exit Sub
Err_Transfer:
MsgBox Err.Description, vbExclamation, "Transfer Failed: Error " & Err.Number
Resume Exit_DoTransfer
End Sub
Last edited: