對於我的表單,幾乎所有的紙張表單都應該帶有一個ID,並將其輸入到一個字段中。但是,一些表單將不帶ID,並且我想根據用戶輸入信息在不同的字段中生成ID號。無法弄清楚如何修復空錯誤
我想要做的是創建一個4位數字,其中第一個數字來自字段usernum,最後3位數字只是按順序計數。
第一次按下按鈕時,usernum字段中沒有任何內容,我認爲這可能是我的運行時錯誤的原因?
下面是我的代碼和錯誤行
Private Sub Command100_Click()
'on click of button InsightId is generated
Me.idnum = NewInsightID()
End Sub
Public Function NewInsightID() As Long
Dim lngNextID As Long
'Find highest ID in the test table and add 1
lngNextID = (Me.usernum * 1000) + DMax([idnum], "test", "usernum=" & Me.usernum) + 1
'ABOVE IS THE LINE WITH ERRORS
'Assign function the value of the Next ID
NewInsightID = lngNextID
End Function
我嘗試了各種解決方法,但沒有工作過
下面是我已經嘗試過,並沒有奏效:
lngNextID = (Me.usernum * 1000) + 1 + NZ(DMax([idnum], "test", "usernum=" & Me.usernum),0)
lngNextID = (Me.usernum * 1000) + 1 + Nz(DMax([idnum], "test", "usernum=" & Nz(Me.usernum, 0)), 0)
lngNextID = (Me.usernum * 1000) + 1 + DMax([idnum], "test", "usernum=" & Nz(Me.usernum, 0))
編輯2: 好的,已經制作好了。除了兩件事情之外,它有一定的作用 - 出於某種原因,它不會從001開始,只有002。之後,它會逐步移動一次。 如果usernum爲空,則最主要的是錯誤。再說一次,我並不預期這種情況,但是當涉及到數據錄入時,我認爲最好假設最可能的用戶,並且因爲我將在這個數據庫被使用時休假,所以我不會想要獲得有關錯誤的任何電話。我試圖這樣做,如果usernum爲空,他們會收到一條消息,告訴他們錯誤並取消該子,但由於某種原因,它不起作用,然後在代碼中,我收到錯誤消息Run查詢表達式'usernum ='中的時間錯誤3075缺少運算符。我假設要做一個消息框來解決這個問題,但我不能讓消息框工作。
Private Sub Command100_Click()
'on click of button InsightId is generated
If usernum = Null Then
Beep
MsgBox ("you cannot leave usernum blank")
Cancel = True
Else: Me.idnum = NewInsightID()
End If
End Sub
Public Function NewInsightID() As Long
Dim lngNextID As Long
'Find highest ID in the test table and add 1
If DMax("idnum", "test", "usernum=" & Me.usernum) = Null Then
lngNextID = (Me.usernum * 1000 + 1)
Else:
lngNextID = 1 + Nz(DMax("idnum", "test", "usernum=" & Me.usernum), Me.usernum * 1000 + 1)
End If
'Assign function the value of the Next ID
NewInsightID = lngNextID
End Function
EDIT3: aaah我解決了它難以置信!
Private Sub Command100_Click()
'on click of button InsightId is generated
If IsNull(Me.usernum) Then
Beep
MsgBox ("you cannot leave usernum blank")
Cancel = True
Else: Me.idnum = NewInsightID()
End If
End Sub
Public Function NewInsightID() As Long
Dim lngNextID As Long
'Find highest ID in the test table and add 1
If DMax("idnum", "test", "usernum=" & Me.usernum) = Null Then
lngNextID = (Me.usernum * 1000 + 1)
Else:
lngNextID = 1 + Nz(DMax("idnum", "test", "usernum=" & Me.usernum), Me.usernum * 1000)
End If
'Assign function the value of the Next ID
NewInsightID = lngNextID
End Function
你的意思是用戶沒有按」在測試表中還沒有任何記錄,但是他們自己有ID?你想要做什麼就像「如果DMax(.....)是空然後lngNextID = Me.usernum * 1000)+ 1否則lngNextID =右(DMax(....),3)+ 1」然而,如果用戶輸入的ID大於此過程創建的ID,則可能會遇到一些問題。 – OpiesDad
什麼是usernum? – justkrys
基本上每個用戶(輸入數據的人)被分配一個數字號碼,它是usernum。 Usernum是計算的ID的第一個數字,即idnum。對於所有用戶,idnum將爲空白,直到他們第一次按下按鈕以指示他們需要生成一個。我會嘗試OpiesDad解決方案,看看它是如何工作的 – eee333