I am trying to see where my issue lies with the following code.
'add data to table
CurrentDb.Execute "INSERT INTO IB Students(Student Name, Gender, IB) " & _
"VALUES (" & Me.Text6 & ",'" & Me.Combo13 & "','" & Me.Text9 & "')"
'refresh data in list on form
stdfrm.Form.Requery
Whenever I try to use the add command I get Runtime Error -3134 and it says there is a syntax error with INSERT INTO statement.
May be the name of table needs to be enclosed by brackets, and text values must be quoted:
'add data to table
sSQL = "INSERT INTO [IB Students] ([Student Name], Gender, IB) " & _
"VALUES ('" & Me.Text6 & "','" & Me.Combo13 & "','" & Me.Text9 & "')"
'print SQL for debug
Debug.print sSQL
'Run query
CurrentDb.Execute sSQL
'refresh data in list on form
stdfrm.Form.Requery
Note: if Me.Text6 (and others) is not bind to data source then you must use it as Me.Text6.Value
Edit: Maybe is a good idea check the length before run the query
When i open the immediate window it shows
INSERT INTO [IB Students] (Student Name, Gender, IB) VALUES (Micheal,'Male','2')
INSERT INTO [IB Students] (Student Name, Gender, IB) VALUES (John,'Male','1')
INSERT INTO [IB Students] ([Student Name], Gender, IB) VALUES (,'','')
INSERT INTO [IB Students] ([Student Name], Gender, IB) VALUES (,'','')
Related
I'm out of my league on this... Another developer before me did something similar to what I want to do by adding a value in a table while updating another table. However, he was running updates as well as inserting, and his primary key was text. Mine PK is integer. Here's his code (works great) that I am trying to reverse engineer and apply to my situation:
Dim sqlQuery As String
sqlQuery = "IF EXISTS (SELECT ReportPK FROM
ACIST_MobilePipelineReportReviewed WHERE ReportPK = '" & ReportPk & "') " &
_
" UPDATE ACIST_MobilePipelineReportReviewed set Status = 'Approved'
WHERE ReportPK = '" & ReportPk & "'" & _
" ELSE " & _
" INSERT INTO ACIST_MobilePipelineReportReviewed ([ReportPK],
[PipelineUID],[ReportDate],[ReportInspector],[Status]) VALUES (" & _
"'" & ReportPk & "','" & Me!PipelineUID & "','" & Me!ReportDate & "','"
& Me!ReportInspector & "','Approved')"
End Sub
Here's what I'm doing: I have a combo box on a form called FacilityEntryF. That form is tied to my FacilityT table. I am selecting "CampaignID" from the CampaignT table and adding it to the FacilityT using that combo box in the aforementioned form. No biggie there... Works great.
The FacilityT has many columns of which I have [FacilityID] which is the primary key and is an autogenerated integer. It also has a column for [CampaignID] which is a foreign key from the CampaignT table.
After adding the CampaignID and starting a new FacilityID in FacilityT, I also want to create a new record in my CampaignFacilityParticipationT table. That table consists of only three columns: CampaignFacilityParticipationID, CampaignID, ParticipantFacilityID. I want to take the new [FacilityID] and the [CampaignID] I added to that table and insert them into the CampaignFacilityParticipationT table (FacilityID goes into the ParticipantFacilityID column). Here's the code below that didn't work (which I'm not surprised because I don't know what I'm doing):
Dim sqlQuery As String
sqlQuery = "IF EXISTS (SELECT FacilityID FROM FacilityT WHERE FacilityID =
" & FacilityID) " & _
" INSERT INTO CampaignFacilityParticipationT ([CampaignFacilityParticipationID],[CampaignID],[ParticipantFacilityID]) VALUES (" & _
"" & CampaignFacilityParticipationID,'" & Me!CampaignID," & Me!ParticipantFacilityID, CampaignID)"
End Sub
Using MS Access 2013 with Microsoft SQL backend.
Thanks!!!
Concatenation is not correct. If apostrophes are needed to delimit parameters then make sure they are used consistently. Normally in Access, apostrophes would only be used for text fields, # for date/time and nothing for number. Maybe because backend is MS SQL apostrophes are needed for all field types? Why do you repeat CampaignID in VALUES clause?
sqlQuery = "IF EXISTS (SELECT FacilityID FROM FacilityT WHERE FacilityID = '" & Me!FacilityID & "')" & _
" INSERT INTO CampaignFacilityParticipationT ([CampaignFacilityParticipationID],[CampaignID],[ParticipantFacilityID])" & _
" VALUES ('" & Me!CampaignFacilityParticipationID & "','" & Me!CampaignID & "','" & Me!ParticipantFacilityID & "')"
Is it possible for you to use a subform on FacilityEntryF on which you select the CampaignID? That will eliminate the need for this code.
CurrentDb.Execute "INSERT INTO convenati(conid, location, surname, firstname, middlename, phone, email, dob, sex, mstatus, status, violated, date)"&_
"VALUES(" & Me.txtid & ", '" & me txtlocation & "','" & Me.txtsurname & "','" & Me.txtmiddlename & "','" & Me.txtfirstname & "','" & Me.txtphone & "','" & Me.txtemail & "','" & Me. txtdate &"','" &_ Me.cbogender &"','" &_ Me.cbomstatus &"','" &_ Me.cboviolated & "','" & Me.txtdate & "')"
Please I need help with this code.
I have a table called convenati, a form called formconvenati and a subform called convenati subform.
In my table I have these fields:
conid
location
surname
firstname
middlename
phone
email
dob
sex
mstatus
status
violated
data.
help me with it
Some of your problems:
you're missing a space after the ).
If any of the fields in the table are field type Date/Time, then there SQL should have # around the date, instead of '
Double-check the number of fields you list in the first half of the query, with the data you're adding (in the last half of the query). One is missing somewhere.
Missing a . after a me..
Put Option Explicit at the top of your modules (first line) to help find & fix undeclared/mishandled variables, objects, etc.
assume you don't have it all squished together like it appears in your example, but if you do, remove all _'s except at the end of the line.
CurrentDb.Execute "INSERT INTO convenati(conid, location, surname, firstname, " & _
"middlename, phone, email, dob, sex, mstatus, status, violated, date) " & _
"VALUES(" & Me.txtid & ", '" & Me.txtlocation & "','" & Me.txtsurname & _
"','" & Me.txtmiddlename & "','" & Me.txtfirstname & "','" & Me.txtphone & _
"','" & Me.txtemail & "','" & Me.txtdate & "','" & Me.cbogender & "','" & _
Me.cbomstatus & "','" & Me.cboviolated & "','" & Me.txtdate & ") "
Also, is the last field you listed in your question date or data?
...the problem all these things have in common is lack of attention to detail. "Close" doesn't count in coding.
One way to troubleshoot problems with SQL being used in VBA is to test the actual SQL output of your statement, in an actual query.
Using the statement above as an example, change CurrentDb.Execute to Debug.Print, and make the next line say Stop. Run your code, and when it breaks at the Stop, hit Ctrl+G to open the Immediate Window, and take a look at the complete SQL.
If you still can't find a problem, copy and paste that output into a Blank Query (in SQL view) and try running the query. It it won't run there either, then you need to start removing parts to simplify the query, troubleshooting by the process of elimination.
I don't know what values each of your form controls hold, nor what data type is expected for each of the fields in the table, so I replaced them all with ___ and got:
INSERT INTO convenati (conid, location, surname, firstname, middlename,
phone, email, dob, sex, mstatus, status, violated, date) VALUES(___,
'___','___','___','___','___','___','___','___','___','___','___')
More debugging tips here.
I'm trying to fix it about hour but it's not work Please help me :(
CurrentDb.Execute "INSERT INTO match_day( home_team, away_team, date, time, home_score, away_score, stadium) " & _
" VALUES('" & Me.textHT & "','" & Me.textAT & "',#" & Me.textDATE & "#,#" & Me.textTime & "#," & Me.textHS & "," & Me.textAS & ",'" & Me.textSTD & ",')"
Are the fields for date and time considered reserved words and should be wrapped in brackets or ticks to qualify it as the column name...
..., [date], [time], ...
But I think it is most likely the trailing final comma before your final closing ) of the inserted values making it look like it wants another field to be inserted.
Me.textSTD & ",')"
change to
Me.textSTD & "')"
I ran into a similar error - thanks to this post I realised that I had used a reserved name "note" in a table ( instead of "notes").
StrSQL = "INSERT INTO option_notes ( OPTION_ID , USER_ID , [NOTE] ) VALUES ( " & currID & " , " & currUserID & " , '" & currNote & "' ) ; "
CurrentDb.Execute StrSQL
I ended up changing the field name - however, wrapping the field name with [ ] allowed the code to execute correctly.
Sorry for the NOOB question I just started with VB. I finally managed to connect my program with my phpmyadmin account, but now I have this little problem.
I am trying to enter the Data the user entered into the form into the db table.
My syntax is wrong. Im doing the following:
Dim SQLStatment As String = "INSERT INTO students(Title, Initial, Surname, Address, City, Country, Postcode) VALUES('" & vtital & vinital & surname & vstreet & vcity & vcountry & vpcode '")"
If anyone could be so kind to give me a brief explanation on how to enter multiple variable data into a db using vb.net it would be much appreciated.
You forgot the separator
It should be like this ..
Dim SQLStatment As String = "INSERT INTO students(Title, Initial, Surname, Address, City, Country, Postcode) VALUES('" & vtital & "','" & vinital & "','" & surname & "','" & vstreet & "','" & vcity & "','" & vcountry & "','" & vpcode & "')"
Later .. use parameterized..
This should work:
Dim SQLStatment As String = String.Format("INSERT INTO students(Title, Initial, Surname, Address, City, Country, Postcode) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
vtital,
vinital,
surname,
vstreet,
vcity,
vcountry,
vpcode)
I have this code:
Set oConnection = Server.CreateObject("ADODB.Connection")
Set oRecordset = Server.CreateObject("ADODB.Recordset")
oConnection.Open "DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;UID=xxxx;PWD=xxxx;DATABASE=xxxx; OPTION=3;"
Sqltemp = "INSERT INTO rsvptable (fname, lname, fbid, rsvp, streetaddress, city, state, zip, streetaddress2, cellphone, acode) " & _
"VALUES ('" & firstName & "', '" & lastName & "', " & fb & ", 0, '" & address1 & "', '" & city & "', '" & strState & "', " & zip & ", '" & address2 & "', " & cell & ", " & returnedNums & ")"
set newAdd = oConnection.execute(Sqltemp)
if newAdd.EOF then
response.write "end"
else
response.write "not end"
end if
And it keeps telling me this:
ADODB.Recordset error '800a0e78'
Operation is not allowed when the object is closed.
/add.asp, line 136
line 136 is this:
if newAdd.EOF then
What would i be overlooking here?
UPDATE
found my answer here! :o)
How to tell if a db update was successful?
two things:
you define oRecordset as the recordset, but then use & check eof on newAdd
Why are you trying to populate a recordet from an insert query?
http://www.w3schools.com/ado/met_conn_execute.asp
The results are stored in a new Recordset object if it is a
row-returning query. A closed Recordset object will be returned if it
is not a row-returning query.
INSERT is not a row-returning query, thus it returns a closed recordset, thus you can't .EOF it. Check the Rows Affected bu passing a variable as the second argument to Execute. If RowsAffected is 1, then you're set.