DCOUNT in Excel VBA

amos5

New Member
Joined
Nov 10, 2011
Messages
1
Hello

I am new to Excel and VBA so excuse me if this makes no sense!

I am trying to import data - Invoices from a MYOB DB (Accounting Software) into Excel then export the Invoice data to an existing Access DB.
I have the import from MYOB sorted and can export the data to the Acces database ok, but are having a problem performing a check for duplicate records in Access before the Insert (The invoice Number from MYOB is going to be the MachineServiceID (PK) in the Access DB. I have read some posts on various forums and it seemed the best way to go was perform a DCOUNT and check if the cell(s) containing the MachineServiceID match the MachineServiceID's already in Access.
After adding the IF statement that includes the DCOUNT I now receive a: Compile Error: Sub or function not defined when trying to run the macro and DCOUNT is highlighted in blue after the error.
I can't work out what is causing the issue, below is my code, if someone could point me in the right direction where I am going wrong or let me know another option other than a DCOUNT would be great.

Thanks
Antony


Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long, i As Long, j As Long, t As String
Dim LTotal As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Users\Ant\Desktop\Database\SMData.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "tblMachineService", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 13 ' the start row in the worksheet
j = 0
t = "MYOB INV: "
Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("MachineServiceID") = Range("A" &r).Value
.Fields("Comments") = t & Range("A" & r).Value
.Fields("DateCompleted") = Range("B" & r).Value
.Fields("ServiceCost") = Range("E" & r).Value
.Fields("Description") = Range("F" & r).Value
.Fields("MachineID") = Range("G" & r).Value
.Fields("CustomerID") = Range("H" & r).Value

If (DCount(.Fields("MachineServiceID"), "tblMachineService") = 0) Then
.Update ' stores the new record
Else
MsgBox "Invoice Number already entered into database"
'error handling to come
End If

End With
r = r + 1 ' next row
j = j + 1 'count total records added

Loop

MsgBox "Number of Invoices Added to Database: " & j & vbCrLf, vbInformation
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

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