我在 MSAccess 表單上有這個復雜的 VBA 函式frm_DataEntry。它搜索不在串列中的值。該函式在組合框的 de NotInList 事件上呼叫。
當組合框中輸入的字串cbo_CustomerLocations不在串列中時,它會tbl_CustomerLocations通過是/否問題詢問我是否要將其添加到表中。之后它從第一列到最后一列并詢問我是否要添加一些新資料。下面的代碼顯示了如何添加一個CustomerLocation.
CustomerID我的表的最后一個欄位tbl_CustomerLocations鏈接到CustomerID表的欄位tbl_Customers
現在我的問題:
當 NotInList 事件被呼叫時,如何更改我的 VBA 代碼,當它到達CustomerID列(最后一列)時,它不能詢問“ What do you want for CustomerID”,而是自動選擇我之前frm_DataEntry在組合框上的同一表單上選擇的 CustomerID cbo_Customers?
Private Sub cbo_CustomerLocationID_NotInList(NewData As String, Response As Integer)
Dim oRS As DAO.Recordset, i As Integer, sMsg As String
Dim oRSClone As DAO.Recordset
Response = acDataErrContinue
String_Value = Me.cbo_CustomerLocationID.Text
MsgBold = String_Value
MsgNormal = "Add to list with locations?"
Debug.Print
If Eval("MsgBox ('" & MsgBold & vbNewLine _
& "@" & MsgNormal & "@@', " & vbYesNo & ", 'New Location')") = vbYes Then
Set oRS = CurrentDb.OpenRecordset("tbl_CustomerLocations", dbOpenDynaset)
oRS.AddNew
oRS.Fields(1) = NewData
For i = 2 To oRS.Fields.Count - 1
sMsg = "What do you want for " & oRS(i).Name
oRS(i).Value = InputBox(sMsg, , oRS(i).DefaultValue)
Next i
oRS.Update
cbo_CustomerLocationID = Null
cbo_CustomerLocationID.Requery
DoCmd.OpenTable "tbl_CustomerLocations", acViewNormal, acReadOnly
Me.cbo_CustomerLocationID.Text = String_Value
End If
結束子
uj5u.com熱心網友回復:
在回圈中使用 If Then 塊來檢查欄位名稱。
If oRS(i).Name = "CustomerID" Then
oRS(i) = Me.cbo_Customers
Else
sMsg = "What do you want for " & oRS(i).Name
oRS(i).Value = InputBox(sMsg, , oRS(i).DefaultValue)
End If
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/372601.html
上一篇:如果行相等,則VBA平均值
