How to save from a form to a table? - vba

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?

Related

How do I solve Access Insert into table error?

I'm doing a project and I just used some code from YouTube where it seemed to work, but it isn't working for me. I am getting this message. :/
Run-time Error: '3134':
Syntax Error in INSERT INTO statement.
Here's the code:
Private Sub Dodaj_Click()
CurrentDb.Execute "INSERT INTO Rezervacije( JMBG, Grad, Broj dana, Broj osoba) " & _
" VALUES('" & Me.JMBG & "', '" & Me.Destinacija & "', '" & Me.Dani & "', '" & Me.Osobe & "')"
CurrentDb.Execute "INSERT INTO Putnici(Broj licne karte, Ime, Prezime, JMBG, Mobilni, Email, Pol) " & _
" VALUES('" & Me.Licna & "', '" & Me.Ime & "', '" & Me.Prezime & "', '" & Me.JMBG & "', '" & Me.Mobilni & "', '" & Me.Email & "', '" & Me.Pol & "')"
End Sub

How to save data from datagridview to sql database without using a button

I need to save data automatically without pressing any button from datagridview to sql database where admission number and names are retrieved from another table and insert score to another table.
For Each row As DataGridViewRow In DataGridView1.Rows
If cmbexam.Text = "CAT 1" And cmbpaper.Text = "Paper 1" Then
sqlSTR = "SELECT AdmNo, Name, Class, Stream, Term, Exam, Subject, Paper, Limit, Score FROM CT11 WHERE (AdmNo = '" & row.Cells(0).Value & "') AND (Name = '" & row.Cells(1).Value & "')"
ExecuteSQLQuery(sqlSTR)
If sqlDT.Rows.Count > 0 Then
sqlSTR = "UPDATE CT11 SET AdmNo ='" & row.Cells(0).Value & "', Name ='" & row.Cells(1).Value & "', Class ='" & cmbform.Text & "', Stream ='" & cmbstream.Text & "', Term ='" & cmbterm.Text & "', Exam ='" & cmbexam.Text & "', Subject ='" & cmbsubject.Text & "', Limit ='" & txtlimit.Text & "', Score ='" & row.Cells(2).Value & "' WHERE (AdmNo = '" & row.Cells(0).Value & "') AND (Name = '" & row.Cells(1).Value & "') AND (Class ='" & cmbform.Text & "') AND (Stream = '" & cmbstream.Text & "') AND (Term = '" & cmbterm.Text & "') AND (Exam = '" & cmbexam.Text & "') AND (Subject = '" & cmbsubject.Text & "')"
ExecuteSQLQuery(sqlSTR)
Else
sqlSTR = "INSERT INTO CT11(AdmNo, Name, Class, Stream, Term, Exam, Subject, SP, Paper, Limit,Score) VALUES ('" & row.Cells(0).Value & "', '" & row.Cells(1).Value & "', '" & cmbform.Text & "', '" & cmbstream.Text & "', '" & cmbterm.Text & "', '" & cmbexam.Text & "', '" & cmbsubject.Text & "', '" & CheckBox2.CheckState & "', 'Paper 1', '" & txtlimit.Text & "','" & row.Cells(2).Value & "')"
ExecuteSQLQuery(sqlSTR)
End If
End If
Next
It's all about Events:
The concept of event-oriented programming is also well suited for the implementation of graphical user interfaces, where the events are mostly user actions, such as pressing a key or clicking a button. Another important field of application are computer simulations, which are set up in such a way that state changes are only triggered by events, and which in turn trigger events (see event-oriented simulation).
Quote from Wikipedia.
I think most developers use the FocusLost-Event to handle Datatransfer if you want to update your database immediately every time you change a value you can try the CellValueChanged-Event.

How to fix syntax error in UPDATE statment in Access VBA

I'm trying to build a database and ran into a problem with the syntax in an UPDATE statement. Does anyone know what's wrong with the statement?
I've tried adjusting the use of single and double quotes but it's still not working.
CurrentDb.Execute "INSERT INTO tbl_SponsorContacts (ContactName, ContactEmail, ContactPhone, SponsorID )" _
& " VALUES ('" & Me.txtContactName & "','" & Me.txtContactEmail & "', '" & Me.txtContactPhone & "' , '" & Me.txtSpnID & "')"
Else
CurrentDb.Execute "UPDATE tbl_SponsorContacts SET ContactName = " _
& " '" & Me.txtContactName & "', ContactEmail = '" & Me.txtContactEmail & "', " _
& " ContactPhone = '" & Me.txtContactPhone & "', SponsorID = '" & Me.txtSpnID & "', WHERE ContactID = '" & Me.txtContactID & "'"
End If
Exit Sub
I keep getting the 3144 syntax error which doesn't provide any hints as to the cause. Any help is appreciated.
UPDATE: Thanks to Jerry M. and forpas for suggesting to remove the comma before where. That helped but I am not getting an error 3061, "Too few parameters. Expected 1".
I will post a new question.
As #forpas mentioned in the comments, it looks like you have an extra ,
Try below:
CurrentDb.Execute "INSERT INTO tbl_SponsorContacts (ContactName, ContactEmail, ContactPhone, SponsorID )" _
& " VALUES ('" & Me.txtContactName & "','" & Me.txtContactEmail & "', '" & Me.txtContactPhone & "' , '" & Me.txtSpnID & "')"
Else
CurrentDb.Execute "UPDATE tbl_SponsorContacts SET ContactName = " _
& " '" & Me.txtContactName & "', ContactEmail = '" & Me.txtContactEmail & "', " _
& " ContactPhone = '" & Me.txtContactPhone & "', SponsorID = '" & Me.txtSpnID & "'WHERE ContactID = '" & Me.txtContactID & "'"
End If
Exit Sub ```

Date exception in German windows during insert MS access vb.net

I have written an insert query which inserts datetime along with other columns. It works fine for all locations except when
my German client logs in and runs the application it gives him below error. I have formatted the datevalue to yyyy-mm-dd to make
culture independent.
MS access database is stored in a server in US.
German client is running application from Germany.
strDateSubmit = dtpDateSub.Value.ToString("yyyy-MM-dd")
strSaveOSTR = "INSERT INTO " & strOSTR & " ([OSTR #],[OSTR Type],[# of Samples],[RA#],[Customer],[SKF #],[Test Description]," & _
"[TestLength],[TestUnit],[TestLengthDays],[Requestor],[Date Submitted],[Seals Avail],[Fixtures Available],[Peripherals Avail],[PO Avail]," & _
"[Machine Type],[Hours to Process],[Location],[Current Status],[ErrorsPresent],[ContaminType]" & SampleREcvd1 & ", [Emp_ID],[Industry])" & _
" Values ( '" & strOSTRNum & "', '" & cmbOSTRTypes.Text & "', " & intSamples & ", '" & strRA & "', '" & strCustomer & "', '" & strSKFNum & _
"', '" & strTestDescr & "', " & intTestLength & ", '" & strTestUnits & "', '" & txtTestLDays.Text & "', '" & strRequestor & "', #" & strDateSubmit & "#, '" & strSealAvail & _
"', '" & strFixtAvail & "', '" & strPheriAvail & "', '" & strPOAvail & "', '" & strMachineClass & "', " & intHrstoProc & ",'" & g_objProp.Location & _
"', '" & strStatus & "', '" & ErrorsPresent & "', '" & ContaminationType & "'" & SampleREcvd2 & ", '" & emp_id & "', '" & Industry & "')"
Error: Syntaxfehler in Datum in Abfrageausdruck '#01.02.2016'
Do yourself a favor: Scrap the dynamic SQL and use a parameterized query, e.g.,
sampleSQL = "INSERT INTO TableName ([Date Submitted]) VALUES (?)"
Dim cmd = New OleDbCommand(sampleSQL, conn)
cmd.Parameters.AddWithValue("?", dtpDateSub.Value)
cmd.ExecuteNonQuery()
That way you can use the actual DateTime value returned by the DateTimePicker. You don't have to be concerned with locale settings or string formats or any of those aggravations.
You may need a more strict format of the date expression string:
strDateSubmit = dtpDateSub.Value.ToString("yyyy'/'MM'/'dd")
This will always return a date like "2016/02/01" (no dots or dashes), and it will concatenate correctly here:
"', #" & strDateSubmit & "#, '"
as you intend.

error in insert command while inserting data into access database

is this command correct for inserting a single value,
"INSERT INTO test (testname) values ('" & txtSelect.Text & "')"
actually i'm trying to insert via this command, but its not working..
"INSERT INTO AdmitPt(Bedcategory, BedNo, BedCharges, PtName, PtAge, Address, PhoneNo,
Date, BloodGroup, Doctor, Remarks) VALUES('" & CmbBedType.SelectedItem & "', '" &
CmbBedNo.SelectedItem & "', " & txtCharges.Text & "', '" & txtPatName.Text & "', '" &
txtPatAge.Text & "', '" & txtPatAdd.Text & "', '" & txtPhone.Text & "', '" &
dtpDate.Value.ToShortDateString & "', '" & cmbBloodGrp.SelectedItem & "', '" &
cmbDoctor.SelectedItem & "', " & txtRemarks.Text & ")"
kindly correct me i'm doing some mistake.
If you posted your entire code - you forgot to actually execute the command. Add
comStudent.ExecuteNonQuery()
before closing the connection.
You should be using Text property of SelectedItem
"INSERT INTO AdmitPt(Bedcategory, BedNo, BedCharges, PtName, PtAge, Address, PhoneNo,
Date, BloodGroup, Doctor, Remarks) VALUES('" & CmbBedType.SelectedItem.Text & "', '" &
CmbBedNo.SelectedItem.Text & "', " & txtCharges.Text & "', '" & txtPatName.Text & "', '" &
txtPatAge.Text & "', '" & txtPatAdd.Text & "', '" & txtPhone.Text & "', '" &
dtpDate.Value.ToShortDateString() & "', '" & cmbBloodGrp.SelectedItem.Text & "', '" &
cmbDoctor.SelectedItem.Text & "', " & txtRemarks.Text & ")"
also ToShortDateString is a method and should be writen as ToShortDateString()
no exception is generated, but values are not going to DB.. not stored..
code
Dim RegNo, BedNo, BedType, Charges, PatName, PatAge, PatAddr, Phone, CheckupDate, BloodGroup, Doctor, Remarks As String
RegNo = txtRegNo.Text
BedNo = CmbBedNo.SelectedItem.ToString()
BedType = CmbBedType.SelectedItem.ToString()
Charges = txtCharges.Text
PatName = txtPatName.Text
PatAge = txtPatAge.Text
PatAddr = txtPatAdd.Text
Phone = txtPhone.Text
CheckupDate = dtpDate.Value.ToShortDateString()
BloodGroup = cmbBloodGrp.SelectedItem.ToString()
Doctor = cmbDoctor.SelectedItem.ToString()
Remarks = txtRemarks.Text
conStudent.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\DBProject\hspms.mdb"
conStudent.Open()
comStudent.CommandText = "INSERT INTO AdmitPt(ID, Bedcategory, BedNo, BedCharges, PtName, PtAge, Address, PhoneNo, Date, BloodGroup, Doctor, Remarks) VALUES('" & RegNo & "', '" & BedType & "', '" & BedNo & "', " & Charges & "', '" & PatName & "', '" & PatAge & "', '" & PatAddr & "', '" & Phone & "', '" & CheckupDate & "', '" & BloodGroup & "', '" & Doctor & "', " & Remarks & ")"
comStudent.Connection = conStudent
comStudent.CommandType = CommandType.Text
conStudent.Close()
code