Private Sub cmdAdd_Click()
'add data to table
CurrentDb.Execute = "INSERT INTO jscbb_dir2(ID,Lastname,FirstName, PrimA, Artea,LubNum,OfficeNum,OfficePhone,Email,LabPhone,stats)" & _
" VALUES(" & Me.Textid & ",'" & Me.TextLast & "','" & Me.TextFirst & "','" & Me.Textprima & "','" & Me.Textarea & "','" & Me.Textlabnum & _
"','" & Me.Textofficenum & "','" & Me.Textofficephone & "','" & Me.Textemail & "','" & Me.Textlabphone & "','" & Me.Textstatus & "')"
'refresh data is list on focus
jscbb_dirsub.Form.Requery
End Sub
Why am I getting an error on the last (Me.Textstatus)? I know this is a low-level question, but I need another pair of eyes, I've been looking at this for over an hour. The error is "Compile Error: Argument Not Optional"
Consider parameters, they will be easier to debug.
Dim qdf As QueryDef
ssql = "INSERT INTO jscbb_dir2(ID,Lastname,FirstName,PrimA,Artea," _
& "LubNum,OfficeNum,OfficePhone,Email,LabPhone,stats) " _
& "VALUES([id],[last],[first],[prima],[area],[lab]," _
& "[office],[phone],[email],[stat])"
Set qdf = CurrentDb.CreateQueryDef("", ssql)
qdf.Parameters("id") = Me.TextID
qdf.Parameters("last") = Me.Textlast
qdf.Parameters("first") = Me.Textfirst
qdf.Parameters("prima") = Me.Textprima
qdf.Parameters("area") = Me.Textarea
qdf.Parameters("lab") = Me.Textlabnum
qdf.Parameters("office") = Me.Textofficenumbet
qdf.Parameters("phone") = Me.Textofficephone
qdf.Parameters("email") = Me.Textemail
qdf.Parameters("stat") = Me.Textstatus
qdf.Execute dbFailOnError
Execute is a method, not a property. You don't use = between a method and its arguments, so
CurrentDb.Execute = "..."
should be
CurrentDb.Execute "..."
Related
So I've got the code below (not all of it is grey for some reason)
Private Sub CmdEdit_Click()
Me.Tableinform.Requery
Dim rs As DAO.Recordset
Set rs = Me.Tableinform.Form.RecordsetClone
'get data to textbox control
With rs
If .RecordCount > 0 Then
With Me.Tableinform.Form.Recordset
Me.ShowIDbox = .Fields("[ID]").Value
Me.CompbyDD = .Fields("[Received By]")
Me.Date1 = .Fields("[Date Received]")
Me.Date2 = .Fields("[Date Processed]")
Me.ReqType = .Fields("[Request Type]")
Me.InsName = .Fields("[Insured Name]")
Me.RiskNo = .Fields("[Risk Number]")
Me.EndtRef = .Fields("[Endorsement Reference]")
Me.EOCNo = .Fields("[EOC Number]")
Me.Tech = .Fields("[Technician]")
Me.BillIns = .Fields("[Billing Instructions]")
Me.Addrecord.Caption = "Update"
Me.CmdEdit.Enabled = False
Me.cmdDuplicate.Enabled = True
Me.CmdDelete.Enabled = True
End With
End If
End With
End Sub
After one Edit and Update, the edit function throws out Error '3021' No Current Record?
Please show me where I am going wrong with this.
Add/Update button as follows:
If Me.ShowIDbox.Value = "" Then
CurrentDb.Execute "INSERT INTO fmdatatable( [Received By], [Date Received], [Date Processed], [Request Type], [Insured Name], [Risk Number], [Endorsement Reference], [EOC Number], [Technician], [Billing Instructions]) " & _
" VALUES ('" & Me.CompbyDD & "','" & Me.Date1 & "','" & Me.Date2 & "','" & Me.ReqType & "','" & Me.InsName & "','" & Me.RiskNo & "','" & Me.EndtRef & "','" & Me.EOCNo & "','" & Me.Tech & "','" & Me.BillIns & "')"
Else
CurrentDb.Execute "UPDATE fmdatatable SET [Received By]='" & Me.CompbyDD & "'" & _
", [Date Received]='" & Me.Date1 & "'" & _
", [Date Processed]='" & Me.Date2 & "'" & _
", [Request Type]='" & Me.ReqType & "'" & _
", [Insured Name]='" & Me.InsName & "'" & _
", [Risk Number]='" & Me.RiskNo & "'" & _
", [Endorsement Reference]='" & Me.EndtRef & "'" & _
", [EOC Number]='" & Me.EOCNo & "'" & _
", [Technician]='" & Me.Tech & "'" & _
", [Billing Instructions]='" & BillIns & "'" & _
" WHERE [ID]=" & Me.ShowIDbox.Value
End If
Me.CompbyDD = "-Please Select-"
Me.Date1 = ""
Me.Date2 = ""
Me.ReqType = "-Please Select-"
Me.InsName = ""
Me.RiskNo = ""
Me.EndtRef = ""
Me.EOCNo = ""
Me.Tech = ""
Me.BillIns = "-Please Select-"
Me.ShowIDbox = ""
Me.Addrecord.Caption = "Add Record"
Me.CmdEdit.Enabled = True
Me.cmdDuplicate.Enabled = False
Me.CmdDelete.Enabled = False
Tableinform.Form.Requery
This edit works once, then all subsequent later edits are throwing errors out.
My ComboBox controls display publishername and authorlastname and stores the publisherid and authorid.
When I run my code, it does display publishername and authorlastname with the ValueMember of publisherid and authorid however when the insert query runs it literally tries to insert the words _publisherid_ and _authorid_.
ComboBox Code:
Private Sub addbook_Load(sender As Object, e As EventArgs) Handles MyBase.Load
mysqlconn = New MySqlConnection
mysqlconn.ConnectionString = "server=localhost;userid=root;database=librarydatabase;Convert Zero Datetime=True"
Dim table As New DataTable
Dim da As New MySqlDataAdapter("select * from publishertable", mysqlconn)
da.Fill(table)
ComboBox1.DataSource = New BindingSource(table, Nothing)
ComboBox1.DisplayMember = "publishername"
ComboBox1.ValueMember = "PublisherId"
Dim pa As New MySqlDataAdapter("select * from authortable", mysqlconn)
pa.Fill(table)
ComboBox2.DataSource = New BindingSource(table, Nothing)
ComboBox2.DisplayMember = "authorlastname"
ComboBox2.ValueMember = "authorid"
End Sub
Insert Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim reader As MySqlDataReader
Try
conn.Open()
query = "SET foreign_key_checks = 0;insert into booktable(ISBNno,bookname,dateofpublication,genre,duodecimal,copies,copiesinstock,authorid,publisherid) Values('" & ISBNno.Text & "','" & Title.Text & "','" & dateofpublication.Text & "','" & genre.Text & "','" & duodecimal.Text & "','" & copies.Text & "','" & copies.Text & "','" & ComboBox2.ValueMember & "', '" & ComboBox1.ValueMember & "');SET foreign_key_checks = 1"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
MessageBox.Show(query)
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
You should be using SelectedValue instead of ValueMember
query = "SET foreign_key_checks = 0;insert into booktable(ISBNno,bookname,dateofpublication,genre,duodecimal,copies,copiesinstock,authorid,publisherid) Values('" & ISBNno.Text & "','" & Title.Text & "','" & dateofpublication.Text & "','" & genre.Text & "','" & duodecimal.Text & "','" & copies.Text & "','" & copies.Text & "','" & ComboBox2.ValueMember & "', '" & ComboBox1.ValueMember & "');SET foreign_key_checks = 1"
should be
query = "SET foreign_key_checks = 0;insert into booktable(ISBNno,bookname,dateofpublication,genre,duodecimal,copies,copiesinstock,authorid,publisherid) Values('" & ISBNno.Text & "','" & Title.Text & "','" & dateofpublication.Text & "','" & genre.Text & "','" & duodecimal.Text & "','" & copies.Text & "','" & copies.Text & "','" & ComboBox2.SelectedValue & "', '" & ComboBox1.SelectedValue & "');SET foreign_key_checks = 1"
So here's my problem I can get my subform to requery once but that is it I can't get it to requery after new information is entered. Can someone please help! I have tried using RecordsetClone and that didn't work as well as other suggestions and I haven't been able to get anything to work
Private Sub cmdAdd_Click()
CurrentDb.Execute "INSERT INTO tbl_GroupVolunteers (GroupName, Leader,
VolunteerName, Email, Phone, EmergencyContact, EmergencyNumber,
NumberofVolunteers, RegistrationNumber)" & _
"VALUES ('" & Me.txtGroupName & "','" & Me.cboLeader & "','" &
Me.txtVolunteerName & "','" & Me.txtEmail & "','" & Me.txtPhone & "','" &
Me.txtEmergencyContact & _
"','" & Me.txtEmergencyNumber & "','" & Me.txtNumberofVolunteers & "','" &
Me.txtRegistrationNumber & "')"
'refresh data
Me!subformGroupVolunteers.Form.Requery
'Clear form
cmdClear_Click
End Sub
Put a break in your code and step through it to see where it's failing. I'd suggest separating the Execute function. Something like this:
Dim sqlStr as String
sqlStr = "INSERT INTO tbl_GroupVolunteers (GroupName, Leader,
VolunteerName, Email, Phone, EmergencyContact, EmergencyNumber,
NumberofVolunteers, RegistrationNumber)" & _
"VALUES ('" & Me.txtGroupName & "','" & Me.cboLeader & "','" &
Me.txtVolunteerName & "','" & Me.txtEmail & "','" & Me.txtPhone & "','" &
Me.txtEmergencyContact & _
"','" & Me.txtEmergencyNumber & "','" & Me.txtNumberofVolunteers & "','" &
Me.txtRegistrationNumber & "')"
CurrentDb.Execute sqlStr
'refresh data
Me!subformGroupVolunteers.Form.Requery
'Clear form
cmdClear_Click
Or instead of stepping through it, you can add a messagebox like this:
Dim sqlStr as String
sqlStr = "INSERT INTO tbl_GroupVolunteers (GroupName, Leader,
VolunteerName, Email, Phone, EmergencyContact, EmergencyNumber,
NumberofVolunteers, RegistrationNumber)" & _
"VALUES ('" & Me.txtGroupName & "','" & Me.cboLeader & "','" &
Me.txtVolunteerName & "','" & Me.txtEmail & "','" & Me.txtPhone & "','" &
Me.txtEmergencyContact & _
"','" & Me.txtEmergencyNumber & "','" & Me.txtNumberofVolunteers & "','" &
Me.txtRegistrationNumber & "')"
Msgbox sqlStr 'Display the string
CurrentDb.Execute sqlStr
'refresh data
Me!subformGroupVolunteers.Form.Requery
'Clear form
cmdClear_Click
This way, you can step through the variable and make sure the string that gets created is functioning properly. If records aren't being added correctly, then it's probably because that INSERT INTO statement isn't pulling in the data properly.
I have the following code. I am using a form in MS Access 2010 to enter new lines into my table Nlog. When I try to run the code, it gives me the error Run-Time error '3061': Too few parameters. Expected 1.
I can't find the error!
Private Sub Command128_Click()
'add data to table
CurrentDb.Execute "INSERT INTO NLog(IDKEY, Company, CoName, State, City, AmtpdTotal, DateRec, Notified, DateNotice, AcctID, TaxType, Period, NoticeReason, Resolution, TaxDue, Intdue, PenDue, Dateres, Amtpdint, amtpdpen, amtpdtax, Assigned, subAssgn, Resolved) " & _
" VALUES(" & Me.TxtIDKEY & ",'" & Me.Company & "','" & Me.CoName & "','" & _
Me.State & "','" & Me.City & "','" & Me.TxtAmtpdTotal & "','" & _
Me.DateRec & "','" & Me.Notified & "','" & _
Me.DateNotice & "','" & Me.AcctID & "','" & Me.TaxType & "','" & Me.Period & "','" & _
Me.NoticeReason & "','" & Me.Resolution & "','" & Me.TaxDue & "','" & Me.IntDue & "','" & Me.PenDue & "','" & _
Me.DateRes & "','" & Me.AmtPdInt & "','" & Me.AmtpdPen & "','" & Me.AmtpdTax & "','" & _
Me.Assigned & "','" & Me.txtsubass & "','" & Me.Resolved & "')", dbFailOnError
MsgBox ("Entry Added")
Use a string variable to hold the INSERT statement. That allows you to Debug.Print the string so you can inspect the actual statement your code created. And you can Execute the string.
Dim strInsert As String
strInsert = "INSERT INTO NLog(IDKEY, Company, CoName, State, City, AmtpdTotal, DateRec, Notified, DateNotice, AcctID, TaxType, Period, NoticeReason, Resolution, TaxDue, Intdue, PenDue, Dateres, Amtpdint, amtpdpen, amtpdtax, Assigned, subAssgn, Resolved) " & _
" VALUES(" & Me.TxtIDKEY & ",'" & Me.Company & "','" & Me.CoName & "','" & _
Me.State & "','" & Me.City & "','" & Me.TxtAmtpdTotal & "','" & _
Me.DateRec & "','" & Me.Notified & "','" & _
Me.DateNotice & "','" & Me.AcctID & "','" & Me.TaxType & "','" & Me.Period & "','" & _
Me.NoticeReason & "','" & Me.Resolution & "','" & Me.TaxDue & "','" & Me.IntDue & "','" & Me.PenDue & "','" & _
Me.DateRes & "','" & Me.AmtPdInt & "','" & Me.AmtpdPen & "','" & Me.AmtpdTax & "','" & _
Me.Assigned & "','" & Me.txtsubass & "','" & Me.Resolved & "')"
Debug.Print strInsert
CurrentDb.Execute strInsert, dbFailOnError
You can then view the completed statement (the output from Debug.Print) in the Immediate window. Ctrl+g will take you to the Immediate window.
If the error is not obvious, you can copy the statement text, create a new query in the query designer, switch it to SQL View, and paste in the copied statement. When you try to run that query from the designer, Access will display a parameter dialog which asks you to supply a value for the parameter. Notice that dialog also includes the "name" of the parameter. Frequently the cause of this problem is a misspelled field name --- since Access can't find a field by that name, it assumes it must be a parameter instead.
Alternatively, you can use VBA code to show you the names of any parameters in your query:
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strInsert)
For Each prm In qdf.Parameters
Debug.Print prm.Name
Next
Solved: i just managed to solve the problem by creating a new table and removing [IT-DEC], [IT-DEC-MAKER-FNAME], [IT-DEC-MAKER-LNAME] where i replaced them by strings accepted by access such as ITDECMAKER ITDECMAKEFNAME ITDECMAKERLNAME
Looks like the problem is solved, however if anyone has a theory to why this has happened id really appreciate your contribution
thank you
I am facing trouble with inserting more than one line of data into my table. For example i load my form and input the data into textbox all information are inserted into their respective tables accordingly, however for the second time when i want to insert data, all data are inserted succesffuly in their respective tables except for one table which is not taking any more data thus only allowing for one line of data.
This is the code i am using in the same form
This part of code is responsible for the userinfo table which is not accepting more than one record
Private Sub proceedBTN_Click()
GlobalVar.flp = Me.qfirstname + Me.qlastname + Me.qmobile
CurrentDb.Execute "INSERT INTO UserInfo(" _
& "FLP, FirstName, LastName, Company, JobTitle, PhoneNumber, Mobile, Email, Fax, " _
& "[IT-DEC], [IT-DEC-MAKER-FNAME], [IT-DEC-MAKER-LNAME], Contact, ContactMethodPhone, " _
& "ContactMethodEmail, ContactMethodFax, ContactMethodPostal , AcquisitionTimeFrame, Budget ) " _
& "VALUES('" & GlobalVar.flp & "','" & Me.qfirstname & "','" & Me.qlastname & "','" & Me.qcompany & "','" & Me.qjob & "','" & Me.qphone & "','" & Me.qmobile & "','" & Me.qemail & "','" _
& Me.qfax & "','" & Me.itdecopt & "','" & Me.qitfirstname & "','" & Me.qitlastname & "','" & Me.contactoption & "','" _
& Me.contactphoneopt & "','" & Me.contactemailopt & "','" & Me.contactfaxopt & "','" & Me.contactpostalopt & "','" & Me.acquisitionoption & "','" & Me.budgetoption & "');"
This Code is responsible for inserting into the UserPartners table which allows succesffuly entering multiple records.
CurrentDb.Execute "INSERT INTO UserPartners(" _
& "FLP, PartnerACT, PartnerBMB, PartnerEverTeam, " _
& "PartnerFormatech, PartnerICC, PartnerIBS, PartnerMegaTek, PartnerMDS, PartnerProcomix, PartnerSetsSolutions, " _
& "PartnerTripleC, PartnerNewHorizons, PartnerPromethean, PartnerTeletrade, PartnerNokia, PartnerPolycom, PartnerDell ) " _
& "VALUES('" & GlobalVar.flp & "','" & Me.partneract & "','" & Me.partnerbmb & "','" _
& Me.partnereverteam & "','" & Me.partnerformatech & "','" & Me.partnericc & "','" & Me.partneribs & "','" & Me.partnermegatek & "','" & Me.partnermds & "','" _
& Me.partnerprocomix & "','" & Me.partnersetssolutions & "','" & Me.partnertriplec & "','" & Me.partnernewhorizons & "','" & Me.partnerpromethean & "','" _
& Me.partnerteletrade & "','" & Me.partnernokia & "','" & Me.partnerpolycom & "','" & Me.partnerdell & "');"
Finally this is the code for the whole form
Option Compare Database
Private Sub contactoption_Click()
If Me.contactoption.Value = 2 Then
Me.contactemailopt.Enabled = False
Me.contactfaxopt.Enabled = False
Me.contactphoneopt.Enabled = False
Me.contactpostalopt.Enabled = False
Me.partneract.Enabled = False
Me.partnerbmb.Enabled = False
Me.partnerdell.Enabled = False
Me.partneredm.Enabled = False
Me.partnereverteam.Enabled = False
Me.partnerformatech.Enabled = False
Me.partneribs.Enabled = False
Me.partnericc.Enabled = False
Me.partnermds.Enabled = False
Me.partnermegatek.Enabled = False
Me.partnernewhorizons.Enabled = False
Me.partnernokia.Enabled = False
Me.partnerpolycom.Enabled = False
Me.partnerprocomix.Enabled = False
Me.partnerpromethean.Enabled = False
Me.partnersetssolutions.Enabled = False
Me.partnerteletrade.Enabled = False
Me.partnertriplec.Enabled = False
Else: Me.contactemailopt.Enabled = True
Me.contactfaxopt.Enabled = True
Me.contactphoneopt.Enabled = True
Me.contactpostalopt.Enabled = True
Me.partneract.Enabled = True
Me.partnerbmb.Enabled = True
Me.partnerdell.Enabled = True
Me.partneredm.Enabled = True
Me.partnereverteam.Enabled = True
Me.partnerformatech.Enabled = True
Me.partneribs.Enabled = True
Me.partnericc.Enabled = True
Me.partnermds.Enabled = True
Me.partnermegatek.Enabled = True
Me.partnernewhorizons.Enabled = True
Me.partnernokia.Enabled = True
Me.partnerpolycom.Enabled = True
Me.partnerprocomix.Enabled = True
Me.partnerpromethean.Enabled = True
Me.partnersetssolutions.Enabled = True
Me.partnerteletrade.Enabled = True
Me.partnertriplec.Enabled = True
End If
End Sub
Private Sub itdecopt_Click()
If Me.itdecopt.Value = 1 Then
Me.qitfirstname.Enabled = False
Me.qitlastname.Enabled = False
Else: Me.qitfirstname.Enabled = True
Me.qitlastname.Enabled = True
End If
End Sub
Private Sub proceedBTN_Click()
GlobalVar.flp = Me.qfirstname + Me.qlastname + Me.qmobile
CurrentDb.Execute "INSERT INTO UserInfo(" _
& "FLP, FirstName, LastName, Company, JobTitle, PhoneNumber, Mobile, Email, Fax, " _
& "[IT-DEC], [IT-DEC-MAKER-FNAME], [IT-DEC-MAKER-LNAME], Contact, ContactMethodPhone, " _
& "ContactMethodEmail, ContactMethodFax, ContactMethodPostal , AcquisitionTimeFrame, Budget ) " _
& "VALUES('" & GlobalVar.flp & "','" & Me.qfirstname & "','" & Me.qlastname & "','" & Me.qcompany & "','" & Me.qjob & "','" & Me.qphone & "','" & Me.qmobile & "','" & Me.qemail & "','" _
& Me.qfax & "','" & Me.itdecopt & "','" & Me.qitfirstname & "','" & Me.qitlastname & "','" & Me.contactoption & "','" _
& Me.contactphoneopt & "','" & Me.contactemailopt & "','" & Me.contactfaxopt & "','" & Me.contactpostalopt & "','" & Me.acquisitionoption & "','" & Me.budgetoption & "');"
CurrentDb.Execute "INSERT INTO UserPartners(" _
& "FLP, PartnerACT, PartnerBMB, PartnerEverTeam, " _
& "PartnerFormatech, PartnerICC, PartnerIBS, PartnerMegaTek, PartnerMDS, PartnerProcomix, PartnerSetsSolutions, " _
& "PartnerTripleC, PartnerNewHorizons, PartnerPromethean, PartnerTeletrade, PartnerNokia, PartnerPolycom, PartnerDell ) " _
& "VALUES('" & GlobalVar.flp & "','" & Me.partneract & "','" & Me.partnerbmb & "','" _
& Me.partnereverteam & "','" & Me.partnerformatech & "','" & Me.partnericc & "','" & Me.partneribs & "','" & Me.partnermegatek & "','" & Me.partnermds & "','" _
& Me.partnerprocomix & "','" & Me.partnersetssolutions & "','" & Me.partnertriplec & "','" & Me.partnernewhorizons & "','" & Me.partnerpromethean & "','" _
& Me.partnerteletrade & "','" & Me.partnernokia & "','" & Me.partnerpolycom & "','" & Me.partnerdell & "');"
CurrentDb.Execute "INSERT INTO UserProducts(" _
& "FLP, ProductsExchange,ProductsLyncServer, ProductsLync , ProductsOffice, ProductsSharePoint, ProductsSharePointInternet, ProductsWindowsServer, " _
& "ProductsSystemCenter, ProductsSQL, ProductsWindows7 ) " _
& "VALUES('" & GlobalVar.flp & "','" & Me.productexchange & "','" & Me.productlyncserver & "','" _
& Me.productlync & "','" & Me.productoffice & "','" & Me.productsharepoint & "','" & Me.productsharepointinternet & "','" & Me.productserver & "','" & Me.productsystemcenter & "','" _
& Me.productsql & "','" & Me.productwindows & "');"
DoCmd.OpenForm "DayChoose", acNormal
DoCmd.Close acForm, "UserInfo", acSaveYes
End Sub
Before I can help you, please rework using parameters as follows:
Private Sub proceedBTN_Click()
Dim Db As DAO.Database
Set Db = CurrentDb
Dim qd As DAO.QueryDef
Dim SQL As String
SQL = "INSERT INTO UserInfo(" & _
"FLP, FirstName, LastName, Company, JobTitle, PhoneNumber, Mobile, Email, Fax, " & _
"[IT-DEC], [IT-DEC-MAKER-FNAME], [IT-DEC-MAKER-LNAME], Contact, ContactMethodPhone, " & _
"ContactMethodEmail, ContactMethodFax, ContactMethodPostal , AcquisitionTimeFrame, Budget) " & _
"VALUES([pflp], [pfirstname], [pqlastname], [pqcompany],[pqjob],[pqphone],[pqmobile], [pqemail]," & _
"[pqfax],[pitdecopt],pqitfirstname,[pqitlastname],[pcontactoption],[pcontactphoneopt],[pcontactemailopt]," & _
"[pcontactfaxopt],[pcontactpostalopt],[pacquisitionoption],[pbudgetoption]);"
Set qd = Db.CreateQueryDef("", SQL)
qd.Parameters("pflp") = GlobalVar.flp
qd.Parameters("pfirstname") = Me.qfirstname
' continue filling parameters....
qd.Parameters("pbudgetoption").Value = Me.budgetoption
qd.Execute
End Sub
If this does not resolve your issue, we can dig a little deeper.