R
Robert
I have a table (Table1) with two fields:
RequisitionNumber (primary key, text)
Activated (YesNo option)
I have a form (Form1)
txtNewRecord (unbound text box)
I have created a recordset on the above table. I need to
find the next available RequisitionNumber where the
Activated value = 0. I have written the code below, tried
to debug it and can't figure out where I went wrong. Any
help in proceeding is appreciated and I thank you in
advance.
Option Compare Database
Option Explicit
Public Sub StartReq()
Dim rst As New ADODB.Recordset
Dim cnn As New ADODB.Connection
Dim num As Integer
Dim varFound As Variant, strCriteria As String,
strActivated As String
Dim strNewReq As String
'Establish connectin and recordset for the current
project
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
Set cnn = CurrentProject.Connection
'Opens the table Table1 and a recordset based on that
table
rst.Open "Table1", cnn, adOpenStatic,
adLockOptimistic, adCmdTable
'Determine the number of records in the recordset
rst.Requery
num = rst.RecordCount
'Test recordset for presence of records.
If rst.RecordCount = 0 Then
MsgBox "There are no requisitions"
rst.Close
Exit Sub
End If
rst.MoveFirst
'Test recordset for available criteria based on
strCriteria
strActivated = "0"
strCriteria = "Activated = '" & strActivated & "'"
rst.Find strCriteria
If Not rst.EOF Then
varFound = rst.Bookmark
rst.Find strCriteria, 1
If rst.EOF Then
rst.Bookmark = varFound
MsgBox "There are no requisitions available"
End If
rst.Close
End If
Forms!form1!txtNewRecord = varFound
End Sub
RequisitionNumber (primary key, text)
Activated (YesNo option)
I have a form (Form1)
txtNewRecord (unbound text box)
I have created a recordset on the above table. I need to
find the next available RequisitionNumber where the
Activated value = 0. I have written the code below, tried
to debug it and can't figure out where I went wrong. Any
help in proceeding is appreciated and I thank you in
advance.
Option Compare Database
Option Explicit
Public Sub StartReq()
Dim rst As New ADODB.Recordset
Dim cnn As New ADODB.Connection
Dim num As Integer
Dim varFound As Variant, strCriteria As String,
strActivated As String
Dim strNewReq As String
'Establish connectin and recordset for the current
project
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
Set cnn = CurrentProject.Connection
'Opens the table Table1 and a recordset based on that
table
rst.Open "Table1", cnn, adOpenStatic,
adLockOptimistic, adCmdTable
'Determine the number of records in the recordset
rst.Requery
num = rst.RecordCount
'Test recordset for presence of records.
If rst.RecordCount = 0 Then
MsgBox "There are no requisitions"
rst.Close
Exit Sub
End If
rst.MoveFirst
'Test recordset for available criteria based on
strCriteria
strActivated = "0"
strCriteria = "Activated = '" & strActivated & "'"
rst.Find strCriteria
If Not rst.EOF Then
varFound = rst.Bookmark
rst.Find strCriteria, 1
If rst.EOF Then
rst.Bookmark = varFound
MsgBox "There are no requisitions available"
End If
rst.Close
End If
Forms!form1!txtNewRecord = varFound
End Sub