通过InputBox输入地址后,在MsgBox中显示最后一个自动生成的文件号



我创建了一个按钮,当按下该按钮时,将创建一个InputBbox,以便在表中输入地址。一旦输入地址,就会生成一个文件号。

我创建的表有一个File_Number列,一旦输入地址,它就会自动编号。

Private Sub Command39_Click()
Dim dbsFileGen As DAO.Database
Dim NewAddress As DAO.Recordset
Dim AddNew As String
Dim FileNum As DAO.Recordset
Dim FileN As Integer
Set dbsFileGen = CurrentDb
Set NewAddress = CurrentDb.OpenRecordset("dbo_File_Generator", dbOpenDynaset, dbSeeChanges)
Set FileNum = CurrentDb.OpenRecordset("dbo_File_Generator", dbOpenDynaset, dbSeeChanges)
AddNew = InputBox("Please enter the building address.")
NewAddress.AddNew
NewAddress!Address = AddNew
NewAddress.Update
End Sub

我尝试了.MoveLast函数来显示表中的最后一个数字,但它显示倒数第二个数字。

好的,因为这只是一个列值,所以输入框的想法是可以的。(但是,对于任何其他字段,您都希望弹出一个数据输入表单作为"对话框"。(;。好的,所以代码必须执行两个步骤:

add the row - save it
THEN get/grab the new auto number ID column

因此,代码应该而且应该是这样的:

Sub AddOneSQL()
Dim AddNew     As String
AddNew = InputBox("Please enter the building address.")

If AddNew = "" Then
' user did not enter anything - exit the routine - bail out
Exit Sub
End If
' if we get here, then user did enter somthing, so we NOW start our data
' operations code. (don't start before the input box, since what then if
' user does not enter, or hits cancel - we bail out as per above.
Dim NewAddress  As DAO.Recordset
Dim MyNewRowPK  As Long

Set NewAddress = CurrentDb.OpenRecordset("dbo_File_Generator", dbOpenDynaset, dbSeeChanges)

NewAddress.AddNew
NewAddress!Address = AddNew
NewAddress.Update           ' this will generate the new auto number id

' now get autonubmer ID - unfortantly DAO has the nasty feature (only when addding)
' that .Update will cause the record poitner to move off (not valid), so we move back
NewAddress.bookMark = NewAddress.LastModified

MyNewRowPK = NewAddress("ID")     ' replace "id" with your autonumber column name
NewAddress.Close

MsgBox "new autonumber id = " & MyNewRowPK

End Sub

最新更新