我有一个问题,我试图通过并打了一堵砖墙,并希望有人可以帮助我。我需要从未绑定的表单写入SQL Server表,但也需要将int主键作为唯一编号的数据的一部分。以下是我的代码:
Option Compare Database
Option Explicit
Private Sub btnSCRID_Click()
Dim db As DAO.Database
Dim rstSCR As DAO.Recordset
Dim SQL As String
SQL = "SELECT [dbo_Master Shared Compliance Review].* FROM [dbo_Master Shared Compliance Review]"
Set db = CurrentDb
Set rstSCR = db.OpenRecordset(SQL, dbOpenDynaset, dbSeeChanges)
If VerifyFields Then
rstSCR.AddNew
rstSCR![User ID] = Me.txtUSERID.Value
rstSCR![Report Date] = Me.txtDate.Value
rstSCR![Partner Name] = Me.txtPartner.Value
rstSCR![Relationship] = Me.cboRelationship.Value
rstSCR![Entity] = Me.cboEntity.Value
rstSCR![Entry Number] = Me.txtEntryNum.Value
rstSCR![ENTRY DATE] = Me.txtEntryDt.Value
rstSCR![Vendor] = Me.txtVendor.Value
rstSCR![PO Number] = Me.txtPO.Value
rstSCR![UPC] = Me.txtUPC.Value
rstSCR![VPN or Style Number] = Me.txtStyle.Value
rstSCR![Item Description] = Me.txtItemDesc.Value
rstSCR![Document Issues] = Me.cboDocIs.Value
rstSCR![Valuation Issue] = Me.cboValIs.Value
rstSCR![OGA Issues(Missing Forms)] = Me.cboOGAis.Value
rstSCR![HTS Issue] = Me.cboHTSis.Value
rstSCR![Comments] = Me.txtComm.Value
rstSCR![Broker Email] = Me.chkBroker.Value
rstSCR![PreAlert Shipment] = Me.chkPreAlert.Value
rstSCR![HTS 1] = Me.txtHTS1.Value
rstSCR![HTS 2] = Me.txtHTS2.Value
rstSCR![HTS 3] = Me.txtHTS3.Value
rstSCR![HTS 4] = Me.txtHTS4.Value
rstSCR![HTS 5] = Me.txtHTS5.Value
rstSCR![HTS 6] = Me.txtHTS6.Value
rstSCR![HTS 7] = Me.txtHTS7.Value
rstSCR![HTS 8] = Me.txtHTS8.Value
rstSCR![HTS 9] = Me.txtHTS9.Value
rstSCR![HTS 10] = Me.txtHTS10.Value
rstSCR![Cost] = Me.txtCost.Value
rstSCR![Assist] = Me.txtAssist.Value
rstSCR![COO] = Me.txtCOO.Value
rstSCR![Manufacturer Name] = Me.txtMFGNM.Value
rstSCR![Manufacturer Address] = Me.txtMFGAD.Value
rstSCR![HTS Description] = Me.txtHTSDESC.Value
rstSCR![Vendor] = Me.txtVendor.Value
rstSCR![SCR ID] = "SCR" & "-" & DatePart("yyyy", Date) & rstSCR![AutoNumber]
rstSCR.Update
rstSCR.Close
Set rstSCR = Nothing
DoCmd.Close acForm, "Batch PO SCR Form"
Forms("Record Search").Controls("btnSavePDF").Visible = True
Else
MsgBox "Cannot create SCR ID becuase required fields are missing. Please see items highlighted in red!", vbOKOnly, "ERROR - CANNOT CREATE ID!"
End If
我的问题是,在下面的最后一行中没有获取要放入字段的SQL int自动编号:
rstSCR![SCR ID] = "SCR" & "-" & DatePart("yyyy",Date) & rstSCR![AutoNumber]
SCR-2018 在SCRID字段中显示正如预期的那样不从SQL Server表中拉入int主键(AutoNumber)。 AutoNumber是Identity(1,1)字段。我没有收到任何错误消息。
答案 0 :(得分:0)
如上所述,要获取触发器,代码等的自动编号和其他字段,您需要保存记录。此外,DAO的一个奇怪的“怪癖”是,当添加记录时,记录指针移动当前记录的OFF(当使用DAO编辑现有记录时不会发生这种情况)。
无论如何,只需强制更新,然后重新设置记录指针,然后拉动+抓取自动编号等。
此代码可以使用:
{{1}}