我昨天發佈了這個消息,之後進行了更改並嘗試了幾件事情來嘗試修復它,但仍然遇到了麻煩。編譯嘗試在Access中使用添加按鈕時發生錯誤
我正在努力工作的數據庫,並試圖將記錄添加到窗體時出現編譯錯誤。
它本質上是一個庫存系統,我有一個表格,裏面有我們倉庫的庫存,我正在試圖創建一個表格,其中包含一個設備上每一條信息的文本框。所以,你去表單,填寫每個文本框中的信息,然後有5個框放在文本框的旁邊,您可以點擊以執行不同的操作。在此之下有一個主庫存表的子表單。
有一個添加,編輯,刪除,清除和關閉按鈕。 「添加」按鈕將記錄添加到此子表格中,並隨後向您的主表格添加您輸入到文本框中的任何信息。要使用「編輯」框,可單擊子窗體中的記錄,然後單擊「編輯」按鈕,它將用所選記錄中的信息填充文本框,以便編輯信息。同時,一旦您單擊編輯,它將添加按鈕上的標籤更改爲更新,以便在編輯完數據後,單擊更新,它將更新子表單和主表中的數據。其他3個按鈕完成他們應有的功能,單擊一條記錄並單擊刪除以刪除記錄,單擊關閉退出該表單並單擊清除以清除填充在文本框中的任何信息。
我遇到的問題是添加/更新按鈕不工作 - 當我點擊它時,我得到這個錯誤:編譯錯誤:找不到方法或數據成員。其他每個按鈕都可以正常工作,而且我已經編寫並重寫了這段代碼兩次,並且無法弄清楚發生了什麼。
最奇怪的部分是,我基本上從另一個數據庫,我編碼的代碼完全按照預期工作,這是超級類似於這一個。切換到這個數據庫時,唯一需要更改的是標籤和表頭的名稱。
任何人都可以幫助我嗎?
代碼:
Option Compare Database
Private Sub cmdAdd_Click()
'when we click on Add button there are two options
'1. for inserting new data
'2. for updating selected data
If Me.txtICN.Tag & "" = "" Then
'add data to table after clicking the add button
CurrentDb.Execute "INSERT INTO tblInventory(ICN, manu, modelNum, serialNum, descr, dateRec, projectNum, dispo, flgDispo, dateRemoved, comments, ulNum, amcaNum)" & _
"VALUES (" & Me.txtICN & ", '" & Me.txtManu & "', '" & Me.txtModel & "', '" & Me.txtSerial & "', '" & Me.txtDesc & "', '" & Me.txtDateRec & "', '" & Me.txtProjectNum & "','" & _
Me.txtDispo & "', '" & Me.chkDispo & "', '" & Me.txtDateRemoved & "', '" & Me.txtComments & "', '" & Me.txtULNum & "', '" & Me.txtAMCANum & "')"
Else
'otherwise
CurrentDb.Execute "UPDATE tblInventory " & _
" SET ICN =" & Me.txtICN & _
", manu ='" & Me.txtManu & "'" & _
", modelNum ='" & Me.txtModel & "'" & _
", serialNum ='" & Me.txtSerial & "'" & _
", descr ='" & Me.txtDescr & "'" & _
", dateRec ='" & Me.txtDateRec & "'" & _
", projectNum ='" & Me.txtProjectNum & "'" & _
", dispo ='" & Me.txtDispo & "'" & _
", flgDispo ='" & Me.chkDispo & "'" & _
", dateRemoved ='" & Me.txtDateRemoved & "'" & _
", comments ='" & Me.txtComments & "'" & _
", ulNum ='" & Me.txtULNum & "'" & _
", amcaNum ='" & Me.txtAMCANum & "'" & _
" WHERE ICN =" & Me.txtICN.Tag
End If
'clear form after data has been added to the table
cmdClear_Click
'refresh data in list on form after form has been cleared
frmInventorySub.Form.Requery
End Sub
Private Sub cmdClear_Click()
Me.txtICN = ""
Me.txtManu = ""
Me.txtModel = ""
Me.txtSerial = ""
Me.txtDesc = ""
Me.txtDateRec = ""
Me.txtProjectNum = ""
Me.txtDispo = ""
Me.chkDispo = ""
Me.txtDateRemoved = ""
Me.txtComments = ""
Me.txtULNum = ""
Me.txtAMCANum = ""
'set focus to ICN number
Me.txtICN.SetFocus
'set edit button to enabled after data has been cleared from form
Me.cmdEdit.Enabled = True
'change caption of button add to Add from Edit
Me.cmdAdd.Caption = "Add"
'clear tag on txtICN
Me.txtICN.Tag = ""
End Sub
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Private Sub cmdDelete_Click()
'delete record
'check existingselected record
If Not (Me.frmInventorySub.Form.Recordset.EOF And Me.frmInventorySub.Form.Recordset.BOF) Then
'confirm delete
If MsgBox("Are you sure you want to delete this record?)", vbYesNo) = vbYes Then
'delete now
CurrentDb.Execute "DELETE FROM tblInventory " & _
"WHERE ICN=" & Me.frmInventorySub.Form.Recordset.Fields("ICN")
'refresh data in list
Me.frmInventorySub.Form.Requery
End If
End If
End Sub
Private Sub cmdEdit_Click()
'check to see if data is already in the form
If Not (Me.frmInventorySub.Form.Recordset.EOF And Me.frmInventorySub.Form.Recordset.BOF) Then
'pull data from selected record into the text boxes
With Me.frmInventorySub.Form.Recordset
Me.txtICN = .Fields("ICN")
Me.txtManu = .Fields("manu")
Me.txtModel = .Fields("modelNum")
Me.txtSerial = .Fields("serialNum")
Me.txtDesc = .Fields("descr")
Me.txtDateRec = .Fields("dateRec")
Me.txtProjectNum = .Fields("projectNum")
Me.txtDispo = .Fields("dispo")
Me.chkDispo = .Fields("flgDispo")
Me.txtDateRemoved = .Fields("dateRemoved")
Me.txtComments = .Fields("comments")
Me.txtULNum = .Fields("ulNum")
Me.txtAMCANum = .Fields("amcaNum")
'store ICN in tag of txtICN in case ICN is modified
Me.txtICN.Tag = .Fields("ICN")
'change caption of add button to Update
Me.cmdAdd.Caption = "Update"
'disable edit button
Me.cmdEdit.Enabled = False
End With
End If
End Sub
是的,我試過了爲好,它讓我在cmdAdd_Click本身是什麼困惑我的編譯錯誤。幾乎就像它找不到cmd或我不知道的東西。 –
**如果Me.txtICN.Tag&「」=「」那麼** 看起來很有趣。你爲什麼要檢查標籤中的值。如果以某種方式將標題編輯爲添加或更新,請使用該標題值在插入和更新功能之間切換。 – geeFlo
(對不起,刪除我的原始答案,因爲它看起來不僅僅是「標籤」的一個實例,而且它很含糊)。另外,如果你的表有自動編號,請注意插入,因爲插入看起來像是在那裏強制一個值。 – geeFlo