Share via

Insert data via vba code to a subform in access

Anonymous
2016-05-25T03:44:01+00:00

I'm pretty close to making it work but for some reason the program won't let me, the error messages that pops out is this 

   1. Syntax error in the INSERT INTO statement(3134) Im pretty sure there's no error here.

   2. 

"Could not find tblClients_subform" 

Here is my code in the block: 

CurrentDb.Execute "INSERT INTO tblClients_subform (ClientID, ClientName, Gender, “& _

        "City, [Address], [Cellphone/Telephone])" & _

        “VALUES (" & Me.txtID & ",'" & Me.txtName & "','" & Me.cboGender & _

        "','" & Me.txtCity & "','" & Me.txtAddress & "','" & Me.txtCellphone & "')"

Is there any other way to enter data from a master from to a subform or table?

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

9 answers

Sort by: Most helpful
  1. Anonymous
    2016-05-25T04:17:13+00:00

    What's the context? Are you using an Unbound Form with controls named txtName and so on? Are you copying data from a mainform to a subform (or the subform's Table)? If so why?

    Secondly, you (at least in words) seem to be mixing Forms with Tables. Forms don't store data; only tables do. Is tblClients_Subform a Form or a Table? If it's a Table, do you have an open subform bound to it?

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. ScottGem 68,830 Reputation points Volunteer Moderator
    2016-05-25T14:24:33+00:00

    You aren't answering my question as to WHY you are doing it this way. it appears you are using Unbound forms. So the question is why. If you are not an experienced Access developer, then Unbound forms are much harder to work with. There are much easier ways.

    As you your Update statement, what datatype is ClientID? Why are you bothering to Update that since it is unlikely to change.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-05-25T13:54:29+00:00

    yeah I managed to make it work, I changed the subform to the table, my error was that I thought that the correct table was the subform created bound to the form, my bad, not that experienced I already fixed it though... Now I'm having trouble with the UPDATE statement, it says syntax error. To me, everything is fine in the syntax

        CurrentDb.Execute "UPDATE tblClients" & _
        "SET ClientID =" & Me.txtID & _
        ", ClientName='" & Me.txtName & "'" & _
        ", Gender='" & Me.cboGender & "'" & _
        ", City='" & Me.cboCity & "'" & _
        ", Cellphone/Telephone='" & Me.txtCellphone & "'" & _
        ", Address (Fisical) ='" & Me.txtAddress & "'" & _
        "WHERE ClientID =" & Me.txtID.Tag
    

    Was this answer helpful?

    0 comments No comments
  4. ScottGem 68,830 Reputation points Volunteer Moderator
    2016-05-25T12:08:11+00:00

    As John said, you don't insert data into forms, you insert into a table that a form is bound to. But I question why this is being done. Since you are pulling the values from controls on a form, the data is already been entered into the form, so where does the subform come into play? Are you using unbound forms? If so, why?

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2016-05-25T04:18:24+00:00

    OK I worked it a bit more and I made it work , the error was that I was trying to use a form (tblClients_subform) as a table....so I switched it back to tblClients and it worked!

    Was this answer helpful?

    0 comments No comments