I am making a database that organizes several hundred CAD drawing filenames. A lot of files have already been named with consecutive numbers, but there are a few gaps of open numbers that I would like the database to fill in first when adding a new drawing file. The format of the filenames is AF####_100_X0. I tried to write some code that goes to the first filename in the table which is AF0000_100_X0 and pulls out the #'s. It then adds 1 to the number and creates a new filename from it AF0001_100_X0. I am having trouble figuring out how to check if that filename is taken or still available. If it is taken, the code has to loop and add 1 again to check for the next file number. If it is available, I want it outputed to a text box on a form. Here is what I have so far:
Dim rst As dao.Recordset
Dim rec As String
Dim nextrec As String
Dim tempstr As String
Dim strcriteria As String
Dim drawnum As Integer
Dim nextdrawnum As Integer
Dim af As String
Dim test As String
Dim lead As String
Dim newfile As Variant
af = "AF"
test = "_100_X0"
Set rst = CurrentDb.OpenRecordset("AirFrame Files")
rst.MoveFirst
'Do
rec = rst![Drawing#].Value
drawnum = Mid(rec, 3, 4)
rst.MoveNext
nextdrawnum = drawnum + 1
If nextdrawnum < 10 Then
lead = "000"
ElseIf nextdrawnum < 100 And nextdrawnum > 9 Then
lead = "00"
ElseIf nextdrawnum < 1000 And nextdrawnum > 99 Then
lead = "0"
ElseIf nextdrawnum < 10000 And nextdrawnum > 999 Then
lead = ""
End If
Str (nextdrawnum)
strcriteria = "[Drawing#]= '" & af & lead & nextdrawnum & test & "'"
newfile = DLookup([Drawing#], "AirFrame Files", "[Drawing#]= '" & af & lead & nextdrawnum & test & "'")
'Loop Until rst.EOF
The loop is commented out because I haven't gotten past the dlookup bugs yet. What is the best way to go about doing this?
Thanks in advance