DMAX in VBA (Excel 2007)

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
DMAX is an Access VBA function not known to Excel VBA. If you put a reference to the Access object library and have your code instantiate an instance of Access, then you can make it work.
 
Upvote 0
Here you go...I am trying to reset when I clear the data after it's been used.

Code:
Private Sub Erase_Data()
'Connect to access
    Set cnn = New ADODB.Connection
    MyConn = "M:\REPORT" & Application.PathSeparator & TARGET_DB
    With cnn
            .Provider = "Microsoft.Jet.OLEDB.4.0"
            .Open MyConn
    End With
'Clear Data from Access
    For i = 1 To 5
            strSql = "DELETE FROM [" & tblName(i) & "]"
            cnn.Execute strSql
    Call ResetSeed(tblName(i))
    Next i
    cnn.Close
    Set cnn = Nothing
End Sub
AND
Code:
Private Function ResetSeed(strTable As String) As String
    Dim strAutoNum As String
    Dim lngSeed As Long
    Dim lngNext As Long
    Dim strSql As String
    Dim strResult As String
 
    lngSeed = GetSeedADOX(strTable, strAutoNum)
    If strAutoNum = vbNullString Then
        strResult = "AutoNumber not found."
    Else
        lngNext = Nz(DMax(strAutoNum, strTable), 0) + 1
        If lngSeed = lngNext Then
            strResult = strAutoNum & " already correctly set to " & lngSeed & "."
        Else
            Debug.Print lngNext, lngSeed
            strSql = "ALTER TABLE [" & strTable & "] ALTER COLUMN [" & strAutoNum & "] COUNTER(" & lngNext & ", 1);"
            Debug.Print strSql
            CurrentProject.Connection.Execute strSql
            strResult = strAutoNum & " reset from " & lngSeed & " to " & lngNext
        End If
    End If
    MsgBox strResult
End Function
AND
Code:
Private Function GetSeedADOX(strTable As String, Optional ByRef strCol As String) As Long
    Dim cat As New ADOX.Catalog
    Dim tbl As New ADOX.Table
    Dim col As New ADOX.Column
 
    Set cat.ActiveConnection = cnn
    Set tbl = cat.Tables(strTable)
 
    For Each col In tbl.Columns
        If col.Properties("Autoincrement") Then
            strCol = "[" & col.Name & "]"
            GetSeedADOX = col.Properties("Seed")
            Exit For
        End If
    Next
 
    Set col = Nothing
    Set tbl = Nothing
    Set cat = Nothing
End Function
 
Upvote 0
I think like this:
Code:
Private Function ResetSeed(strTable As String) As String
    Dim strAutoNum As String
    Dim lngSeed As Long
    Dim lngNext As Long
    Dim strSql As String
    Dim strResult As String
    Dim oAccess as Object
    Set oAccess = CreateObject("Access.Application")
    lngSeed = GetSeedADOX(strTable, strAutoNum)
    If strAutoNum = vbNullString Then
        strResult = "AutoNumber not found."
    Else
        lngNext = Nz(oAccess.DMax(strAutoNum, strTable), 0) + 1
        If lngSeed = lngNext Then
            strResult = strAutoNum & " already correctly set to " & lngSeed & "."
        Else
            Debug.Print lngNext, lngSeed
            strSql = "ALTER TABLE [" & strTable & "] ALTER COLUMN [" & strAutoNum & "] COUNTER(" & lngNext & ", 1);"
            Debug.Print strSql
            CurrentProject.Connection.Execute strSql
            strResult = strAutoNum & " reset from " & lngSeed & " to " & lngNext
        End If
    End If
    MsgBox strResult
oAccess.Quit
Set oAccess=Nothing
End Function
 
Upvote 0
I got the "Reserved Error" on this line. :(

Code:
lngNext = Nz(oAccess.DMax(strAutoNum, strTable), 0) + 1
 
Upvote 0
From searching with the Google Machine, it sounds like it might be a Registry issue...and I'm not wanting to mess with the work computer. :banghead:
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,290
Members
452,902
Latest member
Knuddeluff

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