G
Guest
Hello,
I have an access project with a Table containing about 3000 records, and
growing. The table contains an Id field. The Id is a number which always
starts with the digits 1 or 2.
I want to find the record with the largest Id starting with a specific digit
in the table (for example, the record with the largest Id, from between the
records which have an Id starting with the digit 1).
I am using the following SQL query (where ComputerLetter is the digit I want
the Id to start with and keyfield is the Id field):
subSQL = "SELECT * FROM " & tableName & " t2 " & _
" WHERE (t2." & keyField & " LIKE '" & ComputerLetter & "%') " & _
" AND (len(t2." & keyField & ") > len(" & tableName & "." &
keyField & "))"
getMaxId_SQL = _
"SELECT max(" & tableName & "." & keyField & ") AS maxId" & _
" FROM " & tableName & _
" WHERE " & _
" (" & tableName & "." & keyField & _
" LIKE '" & ComputerLetter & "%')" & _
" AND NOT EXISTS (" & subSQL & ")"
After that I use a function which returns the first value (denoted maxId) of
the first column, returned by the SQL query statement, and then I do:
pureMaxId = Val(Mid(maxId, 2, Len(maxId) - 1))
The point is that when debugging the code line by line, the part which takes
very long is the running of the SQL query.
any ideas why and how to make it run faster?
thank you.
I have an access project with a Table containing about 3000 records, and
growing. The table contains an Id field. The Id is a number which always
starts with the digits 1 or 2.
I want to find the record with the largest Id starting with a specific digit
in the table (for example, the record with the largest Id, from between the
records which have an Id starting with the digit 1).
I am using the following SQL query (where ComputerLetter is the digit I want
the Id to start with and keyfield is the Id field):
subSQL = "SELECT * FROM " & tableName & " t2 " & _
" WHERE (t2." & keyField & " LIKE '" & ComputerLetter & "%') " & _
" AND (len(t2." & keyField & ") > len(" & tableName & "." &
keyField & "))"
getMaxId_SQL = _
"SELECT max(" & tableName & "." & keyField & ") AS maxId" & _
" FROM " & tableName & _
" WHERE " & _
" (" & tableName & "." & keyField & _
" LIKE '" & ComputerLetter & "%')" & _
" AND NOT EXISTS (" & subSQL & ")"
After that I use a function which returns the first value (denoted maxId) of
the first column, returned by the SQL query statement, and then I do:
pureMaxId = Val(Mid(maxId, 2, Len(maxId) - 1))
The point is that when debugging the code line by line, the part which takes
very long is the running of the SQL query.
any ideas why and how to make it run faster?
thank you.