这会从表格Voucher_Number
tblInvoiceLog
字段中获取最大数量
我想获得最大数量,但仅限于同一表格中的字段Source
等于Me.Source.Value
我不知道如何添加此条件请告知:
CODE:
Private Sub Source_AfterUpdate()
Dim rs As ADODB.Recordset, MyVal
Set rs = New ADODB.Recordset
rs.Open "SELECT MAX(Voucher_Number) from tblInvoiceLog", CurrentProject.Connection
rs.MoveFirst
MyVal = rs.Fields(0).Value
Me.Voucher_Number.Value = MyVal + 1
rs.Close
Set rs = Nothing
End Sub
答案 0 :(得分:2)
您应该在SQL查询中添加WHERE子句
Private Sub Source_AfterUpdate()
Dim rs As ADODB.Recordset, MyVal
Dim SQL as String
Set rs = New ADODB.Recordset
' If Source field is STRING type
SQL = "SELECT MAX(Voucher_Number) from tblInvoiceLog WHERE [Source]='" & Me.Source.Value & "'"
' If Source field is NUMBER type
SQL = "SELECT MAX(Voucher_Number) from tblInvoiceLog WHERE [Source]=" & Me.Source.Value
rs.Open SQL, CurrentProject.Connection
rs.MoveFirst
MyVal = rs.Fields(0).Value
Me.Voucher_Number.Value = MyVal + 1
rs.Close
Set rs = Nothing
End Sub
答案 1 :(得分:1)
Private Sub Source_AfterUpdate()
Dim rs As ADODB.Recordset, MyVal
Set rs = New ADODB.Recordset
rs.Open "SELECT MAX(Voucher_Number) from tblInvoiceLog where source="&me.source.value, CurrentProject.Connection
rs.MoveFirst
MyVal = rs.Fields(0).Value
Me.Voucher_Number.Value = MyVal + 1
rs.Close
Set rs = Nothing
End Sub