Insert SQL from combobox/textbox selections - sql

I'm trying to add a row of data into the table using the selections from comboboxes and the textlabel. The error I'm receiving is in regard to the insert statement. Is this the correct way to reference the values in a combobox or textbox? I'm inserting into a table that has an autonumber primary key. Any help would be greatly appreciated.
Private Sub addResult_Click()
Dim strSQL As String
strSQL = "INSERT INTO Results (Student_ID, Class_ID, Test_Type, Student_Score, Total_Score, Level)
VALUES ('" & Me.cboStudents.Column(0) & "',
'" & Me.cboTeacher.Column(0) & "',
'" & Me.cboTestType.Column(1) & "',
'" & Me.txtTotalScore.Value & "',
'" & Me.txtStudentScore.Value & "',
'" & Me.cboTeacher.Column(2) & "')"
CurrentDb.Execute strSQL
End Sub

Related

How to save from a form to a table?

I have a form InventoryForm and a table StockRecords. I'm trying to get the records in the form to save to the table AfterUpdate. Both my form and table have fields EmployeeName, PartNo, Quantity, BinNo, Supplier, Shelf, Description, Price & TargetStockLevel.
The form AfterUpdate's VBA code (that I've seen working for others online but not for me) is:
Private Sub Form_AfterUpdate()
CurrentDb.Execute "INSERT INTO StockRecord (EmployeeName, PartNo, Quantity, BinNo, Supplier, Shelf, Discription, Price, TargetStockLevel) VALUES ('" & Me.SelectName & "', '" & Me.PartNo & "', '" & Me.Quantity & "', '" & Me.BinNo & "', '" & Me.Supplier & "','" & Me.Shelf & "', '" & Me.Discription & "', '" & Me.Price & "', '" & Me.TargetStockLevel & "' )"
End Sub
Edit :
Private Sub Form_AfterUpdate()
'add data to table
CurrentDb.Execute "INSERT INTO StockRecord(EmployeeName, PartNo, Quantity, BinNo, Supplier, Shelf, Discription, Price, TargetStockLevel) " & _
" VALUES ('" & Me.cboSelectName & "', '" & Me.cboPartNo & "','" & _
Me.txtQuantity_Entered & "', '" & Me.txtBinNo & "', '" & Me.txtSupplier & "','" & Me.txtShelf & "', '" & Me.txtDiscription & "', '" & Me.txtPrice & "', '" & Me.txtTargetStockLevel & "' )"
End Sub
This new VBA code worked once but now won't (even when I made no changes). I get a message saying :
Compile Error : method or data member not found.
All fields are text boxes, except two combo boxes (SelectName & PartNo). How to fix this?

VB.NET 2010 Inserting data to table in access

PLease I need help whenever I register my form to database, it says registration successful but the last table (the TRANSACTION table) data information from my form does not input the info in my database only the table FORM and STUDENT have data from my form. Is there something wrong in my TRANSACTION code? or in database?
PLEASE HELP :((
sql = "INSERT INTO FORM VALUES ('" & txtformnum.Text & "' , '" & bcboRequest.Text & "' , '" & txtTotal.Text & "')"
da = New OleDb.OleDbDataAdapter(sql, con) '"
da.Fill(ds, "FORM")
sql = "INSERT INTO STUDENTS VALUES ('" & txtstudnum.Text & "','" & txtSurname.Text & "','" & txtGName.Text & "', '" & txtMName.Text & "', '" & txtAddress.Text & "', '" & status & "' , '" & txtYr.Text & "' , '" & cbostype.Text & "' , '" & chkClearance.Text & "', '" & txtCourse_Track.Text & "' , '" & txtCNumber.Text & "' , '" & dot.Value & "' , '" & dotdue.Value & "')"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "STUDENTS")
Dim sqlquery As String = "INSERT INTO TRANSACTION (Transaction_num,Stud_num,Form_num,Total Fee)" + "VALUES (" & txttransactionno.Text & ",'" & txtstudnum.Text & "'," & txtformnum.Text & "," & txtTotal.Text & ");"
Dim sqlcommand As New OleDb.OleDbCommand(sqlquery)
I don't know what database you're using, but if you're using MySQL or MS SQL Server then the keyword TRANSACTION is reserved and must be escaped to work within your statement.
If using MySQL, try changing your statement to INSERT INTO "TRANSACTION"
If using SQL Server, change your statement to INSERT INTO [TRANSACTION]
If you're not using either of those, post what database system you're using and I'll post the proper escape syntax.
Try changing this line
Dim sqlquery As String = "INSERT INTO TRANSACTION (Transaction_num,Stud_num,Form_num,Total Fee)" + "VALUES (" & txttransactionno.Text & ",'" & txtstudnum.Text & "'," & txtformnum.Text & "," & txtTotal.Text & ");"
To
Dim sqlquery As String = "INSERT INTO TRANSACTION ([Transaction_num],[Stud_num],[Form_num],[Total Fee]) " + "VALUES ('" & txttransactionno.Text & "','" & txtstudnum.Text & "','" & txtformnum.Text & "','" & txtTotal.Text & "');"
This should work fine.
Add the following to your code:
sqlcommand.ExecuteNonQuery()

Run time 3134 error MS Access SQL

Private Sub AddItemButton_Click()
Dim db As Database
Set db = CurrentDb
Dim Item As String
Dim Rate As String
Dim AltRate As String
Dim Reason As String
Dim ApporvedBy As String
Dim Company As String
Dim JobID As String
Me.JobID2 = Me.JobID
db.Execute "INSERT INTO InvItemsRecords (Item, Rate, AltRate, Reason, ApprovedBy, Company, JobID) VALUES ('" & Me.ItemAppendCBO & "', " & Me.RateAppend & ", " & Me.AltRateAppend & ", '" & Me.ReasonAppend & "', '" & Me.ApprovedByAppend & "', '" & Me.Customer & "', " & Me.JobID2 & ")"
Me.AltRate2.Value = Null
End Sub
You should use string literals for your Queries
Dim sqlQuery As String
sqlQuery = "INSERT INTO InvItemsRecords (Item, Rate, AltRate, Reason, ApprovedBy, " & _
"Company, JobID) VALUES ('" & Me.ItemAppendCBO & "', " & Me.RateAppend & ", " & Me.AltRateAppend & ", '" & Me.ReasonAppend & "', '" & _
Me.ApprovedByAppend & "', '" & Me.Customer & "', " & Me.JobID2 & ")"
db.Execute sqlQuery
It makes it a lot easier to debug
INSERT INTO InvItemsRecords (Item, Rate, AltRate, Reason, ApprovedBy, Company, JobID)
VALUES('Some Item', 100, 343, 'Reason A', 'John', 'Jane Doe', Null)
Chances are you are inadvertently passing 1 or more null values to non-nullable fields. Also be careful about the data types. You may need to use CStr(), CInt(), CLong, CDate() or ... to cast your data into the right format If a field can be left blank us NZ().
NZ(Me.RateAppend, True)

Access VBA to repeat a specific line of code

I have tried searching for this, but cannot find anything quite like what I want to do... Funny, because I would think it would be pretty simple.
I have the following code:
Dim strSQL As String
Dim strSQL1 As String
strSQL1 = [Reports]![SMT_PULL_SHEET]![Text59].Value
strSQL = "INSERT INTO tblJobRecordID (Job, JobRev, JobDate, JobTime) VALUES ('" & strSQL1 & "', '" & Me!Text63.Value & "', '" & Format(Now(), "MM/DD/YY") & "', '" & Format(Now(), "h:mm AMPM") & "');"
DoCmd.RunSQL (strSQL)
This logs information for manufacturing products. The table tblJobRecordID creates a unique identifier by means of an autonumber field that acts as a way to serialize each product.
I want to run DoCmd.RunSQL (strSQL) a certain number of times based on a text box populated on a report.
The intention is to insert the same record into the table a certain number of times and give each record a unique identifier in the autonumber field.
I am familiar with loops and do not want to use this method if I can help it. Is there a simple command that tells Access to run a line of code a specific number of times?
Any help is very much appreciated. Thank you!
Dim strSQL As String
Dim strSQL1 As String
strSQL1 = [Reports]![SMT_PULL_SHEET]![Text59].Value
strSQL = "INSERT INTO tblJobRecordID (Job, JobRev, JobDate, PrintTime, LotQty) VALUES ('" & strSQL1 & "', '" & Me!Text63.Value & "', '" & Format(Now(), "MM/DD/YY") & "', '" & Format(Now(), "h:mm AMPM") & "', '" & Me!Text67.Value & "');"
DoCmd.SetWarnings False
For intCounter = 1 To [Reports]![SMT_PULL_SHEET]![Text67].Value
DoCmd.RunSQL (strSQL)
Next
DoCmd.SetWarnings True`
Easier than I thought to prove out the loop for this. Thanks gentleman!

Why do I get a data type mismatch when trying to update an Access database ? ( with autonumber)

I'm trying to update a customer table from VB.NET with the following SQL statement:
Dim SQLstr As String = "UPDATE tblKlant SET NaamFirma='" & naamFirma & "', Straat='" & straat & "', Nummer = '" & nummer & "', Gemeente='" & gemeente & "', Postcode='" & postcode & "', TEL='" & tel & "', Fax='" & fax & "',Gsm='" & gsm & "', BtwNr='" & btwNr & "', Email ='" & email & "', Website='" & website & "', IBAN ='" & IBAN & "', BIC='" & BIC & "' WHERE ID='" & CInt(id) & "';"
mainForm.SQLcommand(SQLstr)
The error lies in the WHERE ID='" & CInt(id) & "';" part. This gives me an error:
Data type mismatch in criteria expression
Cause when I replaced it with something else the query worked. The ID is an autonumber by the way. I know you can't update an autonumber but you must be able to use it in the WHERE clause.
Thanks in advance