(Please see my comments interspersed with the quoted message.)
Debra Ann said:
Dirk, I decided to go a different route than have two tables and having to
update a different table to keep track of a number system. I have written
the code to help you understand what I am trying to do (forgot what I said
before about update a table not open). The only thing I cannot figure out
now is how to query a table, sort a field in descending order and place
the
number in that field into a variable that I will use to populate a field
in
my form. Here is the code:
Dim strField1 As String
Dim StrField2 As String
Dim StrField3 As String
Dim StrField4 As String
Dim varLookup As Variant
‘I am in a form and I have clicked a command button to get the next
Sequence
Number depending on the uniqueness of Fields 1 through 4
Me.cboField1.SetFocus
strField1 = Me.cboField1.Text
Me.cboField2.SetFocus
StrField2 = Me.cboField2.Text
Me.cboField3.SetFocus
StrField3 = Me.cboField3.Text
Me.cboField4.SetFocus
StrField4 = Me.cboField4.Text
This code can be improved. In Access, the .Text property of a control has
very limited use, and requires that the control have the focus when you
refer to it. Instead, most code should use the .Value property, which is
the control's default property. Better code for capturing the values of the
combo boxes is:
strField1 = Me.cboField1
StrField2 = Me.cboField2
StrField3 = Me.cboField3
StrField4 = Me.cboField4
‘This is what I can’t figure out. I want to query the log dependent on the
matchup of the four other fields, sort it descending order, and pick the
number that is in the [Sequence Number] field. The is the sql if I were
to
query but not sure how to put it in VB code:
varLookup = "SELECT TOP 1 tbl_LOG.[Sequence Number] FROM tbl_LOG WHERE
(((tbl_LOG.[Field1]) = strField1) And ((tbl_LOG.[Field2]) = StrField2) And
((tbl_LOG.[Field3]) = StrField3) And ((tbl_LOG.[Field4]) = StrField4))
ORDER
BY tbl_LOG.[Sequence Number] DESC;"
‘Fill in the Sequence_Number field on the form
If varLookup Is Null then
Me.Sequence_Number.text = “50001â€
Else
Me.Sequence_Number.text = varLookup + 1
End If
Sorry for the confusion. Any help what the query part would be greatly
appreciated.
What you want here is the maximum sequence number. To get that, you could
open a recordset on the appropriate SQL statement, but for your purpose it's
probably simpler to use the DMax() function:
Const Q As String = """"
varLookup = _
DMax("[Sequence Number]", "tbl_Log", _
"[Field1]=" & Q & strField1 & Q & " AND " & _
"[Field2]=" & Q & StrField2 & Q & " AND " & _
"[Field3]=" & Q & strField3 & Q & " AND " & _
"[Field4]=" & Q & strField4 & Q)
If IsNull(varLookup) Then
Me.Sequence_Number = 50001
Else
Me.Sequence_Number = varLookup + 1
End If
Note that I have assumed that your fields Field1, Field2, Field3, and Field4
are text fields. If they aren't, then the quoting in the Criteria argument
must be adjusted.
--
Dirk Goldgar, MS Access MVP
Access tips:
www.datagnostics.com/tips.html
(please reply to the newsgroup)