INSERT INTO - errors, but allows input into table

谁说胖子不能爱 提交于 2020-06-01 07:39:06

问题


For reasons I cannot see I get the following error message:

Compile error: Method or data member not found

when I use the following:

Private Sub cmd_Add_Click()

Dim strSQL As String

strSQL = " INSERT INTO BERTHAGE " _
& "(BOAT, LOCATION, BERTH_WEEK, BERTH_YEAR, BERTHED) VALUES " _
    & Me.Add_Boat & "','" _
    & Me.LOCATION & "','" _
    & Me.txt_week & "','" _
    & Me.txt_year & "','" _
    & Me.In_Port & "');"

cmd_Clear_Click

End Sub

Once I click OK and use the refresh button the entry is put into the database, but each time I do an entry I have to go to the same process.

I would like to figure out what method or data is missing?

I should add that there is an outnumber primary key field on this table (Berth_ID), and each time I use the cmd_Add button a new ID number is created for the new record. This includes creating a new ID number for the new record that triggers the error.

Here is all the VBA associated with this form

Private Sub Form_Load()
DoCmd.RunCommand acCmdRecordsGoToLast
End Sub

Private Sub LOCATION_Change()
    Me.txt_Cur_Flo = Me.LOCATION.Column(1)
    Me.txt_Cur_Doc = Me.LOCATION.Column(2)
    Me.txt_Cur_Ori = Me.LOCATION.Column(3)
End Sub

Private Sub cmd_Add_Click()

Dim strSQL As String

strSQL = " INSERT INTO BERTHAGE " _
& "(BOAT, LOCATION, BERTH_WEEK, BERTH_YEAR, BERTHED) VALUES " _
    & Me.Add_Boat & "','" _
    & Me.LOCATION & "','" _
    & Me.txt_week & "','" _
    & Me.txt_year & "','" _
    & Me.In_Port & "');"

cmd_Clear_Click

End Sub

Private Sub cmd_Clear_Click()
Me.Add_Boat = ""
Me.LOCATION = ""
Me.txt_Cur_Flo = ""
Me.txt_Cur_Doc = ""
Me.txt_Cur_Ori = ""  
Me.Add_Boat.SetFocus
End Sub

Private Sub cmd_Close_Click()
DoCmd.Close
End Sub

回答1:


Consider the best practice of parameterization and not string concatenation of SQL mixed with VBA variables. Due to missing quotes, the compiler attempts to reference a column name and not its literal value. Instead, consider parameterization with defined types which is supported with Access SQL using QueryDefs. Notice below, SQL and VBA are complete separate.

SQL (save as stored query)

PARAMETERS prmBoat TEXT, prmLoc INT, prmBerthed INT;
INSERT INTO BERTHAGE (BOAT, LOCATION, BERTHED)
VALUES(prmBoat, prmLoc, prmBerthed)

VBA

Dim db As Database
Dim qdef As QueryDef
Dim strSQL As String

Set db = CurrentDb
Set qdef = db.QueryDefs("mySavedParamQuery")

' BIND PARAM VALUES
qdef!prmBoat = Me.Add_Boat
qdef!prmLoc = Me.LOCATION
qdef!prmBerthed = Me.In_Port

' EXECUTE ACTION QUERY
qdef.Execute

Set qdef = Nothing
Set db = Nothing

Even better, save your query with form controls intact and simply call OpenQuery:

SQL (save as stored query)

INSERT INTO BERTHAGE(BOAT, LOCATION, BERTHED)
VALUES(Forms!myForm!Add_Boat, Forms!myForm!LOCATION, Forms!myForm!In_Port)

VBA

Private Sub cmd_Add_Click()
   Dim strSQL As String

   DoCmd.SetWarnings False                   ' TURN OFF APPEND PROMPTS
   DoCmd.OpenQuery "mySavedActionQuery"
   DoCmd.SetWarnings True                    ' RESET WARNINGS

   Call cmd_Clear_Click

End Sub



回答2:


Missing opening parenthesis after VALUES. Also missing apostrophe in front of Me.Add_Boat. These special characters must always be in pairs, an even number by counting.

If Berth_Week and Berth_Year are number fields (and should be), don't use apostrophe delimiters.

If In_Port is a Yes/No field, don't use apostrophe delimiters.




回答3:


The issue appears to be that I was doubling up the inputs into the 'week' and 'year' field. this was happening (I believe) because those text box fields were already accessing the week and year information directly from the default value on the BERTHAGE table. Essentially I went through each input and would run it individually waiting for the error to occur. Once it occurred I took it out of the INSERT INFO statement. With the removal of week and year, everything is working. That was a painful exercise, and still not complete, but I am back to a function form/DB so I'll take the small victories when they occur.

Private Sub cmd_Add_Click()

Dim strSQL As String
CurrentDb.Execute " INSERT INTO BERTHAGE " & "(BOAT, LOCATION, BERTHED) VALUES ('" &     Me.Add_Boat & "'," _
    & Me.New_Loc & "," _
    & Me.In_Port & ");"

cmd_Clear_Click

DoCmd.Requery

End Sub`


来源:https://stackoverflow.com/questions/62026993/insert-into-errors-but-allows-input-into-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!