I can not compile database - ms-access

Problem: When user open the split database they get an error telling that the VBA code is bad. If they proceed the codes in the modules get wiped out. Based on what I have read, I need to compile the database but I am stuck! How do I fix the issue below
Private Sub Sub_Location_GotFocus()
Dim Location_Filter As Variant
Location_Filter = Me.Location
Me.Sub_Location.RowSource = "SELECT Sub_Location.Sub_Location" _
& " FROM Sub_Location" _
& " WHERE (((Sub_Location.Location) = '" & Location_Filter & "'))" _
& " ORDER BY Sub_Location.Sub_Location;"
End Sub

Related

MS Access - Creating a module that filter subform by different comboboxes using VBA

I have a form with "some" comboboxes and 1 subform that is currently filtered just by the combobox1 with the following VBA code:
Private Sub cmbType_AfterUpdate()
Dim strSQL As String
strSQL = "SELECT [qryStore].[Type], [qryStore].[Model], [qryStore].[SN], " _
& "[qryStore].[ID], [qryStore].[Position], " _
& "FROM qryStore " _
& "WHERE (((qryStore.Type)='" & Me.cmbType & "'));"
Me.subfrmStore.Form.RecordSource = strSQL
Me.subfrmStore.Form.Requery
End Sub
I want to turn this code in a module so i can Call the module once for all the comboboxes of the form instead of duplicate this code for each individual combobox..
How can i achieve that?!
Here:
Public Sub UpdateSubFormFromControl(ByRef ControlName as String)
Dim strSQL As String
strSQL = "SELECT [qryStore].[Type], [qryStore].[Model], [qryStore].[SN], " _
& "[qryStore].[ID], [qryStore].[Position], " _
& "FROM qryStore " _
& "WHERE (((qryStore.Type)='" & Forms!MyFormName.Controls(ControlName).Value & "'));"
End Sub
This allows you to pass the control name to a sub and do the same exact thing. However, you'll notice I had to fully qualify the form. I think its a better practice to always fully qualify (specifically for reasons such as yours).
I also left out the requery logic - either add it to this or leave it in the code that calls it.
Enjoy!

ms access error 3061 - To Few Parameters

I'm very new to coding and ms acess, so I really need your help.
I'm trying to get a button to update some data in a table.
I used this code but I keep getting this 3061 error, and i'm going crazy :( help!
Private Sub Concluido_Click() 'Add
CurrentDb.Execute "UPDATE OFP_tempos " & _
" SET Id=" & Me.txtId & _
", Tempo_total='" & Me.txtTotal & "'" & _
", Fim='" & Me.txtHora & "'" & _
" WHERE OFP_tempos=" & Me.txtId.Tag
' disable button edit
Me.Concluido.Enabled = False
End Sub
OK so I found the error at last!
(Now I feel stupid for asking -.-')
On the last line I had the name of the table OFP_tempos instead of the ID -.-'

DoCmd.RunSQL mySql got Run-time error '3464'

I have this simple code of vba access to update product in the database. But when I debug, it stops at the DoCmd statement and got run-time error. I've made research about this kind of error and code, and had changed the code but still caused an error. Below is my simple code to update the product value.
Sub UpdateProduct()
Dim mySql As String
mySql = "UPDATE " & Forms!UPDATE_PRODUCT!cbxLensType _
& " SET LOT_NO = " & Forms!UPDATE_PRODUCT!txtLotNo _
& " WHERE EAN_CODE = " & Forms!UPDATE_PRODUCT!txtEan & ";"
DoCmd.RunSQL mySql
End Sub
Could you help me to explain what is the problem to my code? Is it because of the update syntax?
Thanks in advance.
**New to access vba
Since EAN_CODE is Text type you need to enclose it inside single quotes.
Sub UpdateProduct()
Dim mySql As String
mySql = "UPDATE [" & Forms!UPDATE_PRODUCT!cbxLensType _
& "] SET LOT_NO = " & Forms!UPDATE_PRODUCT!txtLotNo _
& " WHERE EAN_CODE = '" & Forms!UPDATE_PRODUCT!txtEan & "';"
DoCmd.RunSQL mySql
End Sub
If LOT_NO is also a Text type, make sure that it is also enclosed in Single quotes.

VB.net update query is not giving errors and not updating my sql database

Dim conntps As MySqlConnection
Dim myconnstringtps As String
conntps = New MySqlConnection()
Dim mycommand As New MySqlCommand
Dim Updatepayments As String = "update payments set payments.payorname='" & _
epayorname.Text & "', payments.cardnumber='" & eccnumber.Text & _
"', payments.bankname='" & ebankname.Text & "', payments.checkaccountnumber='" & _
eaccountnumber.Text & "', payments.checkroutingnumber='" & _
erouting.Text & "', payments.cardexpirationdate='" & eexpmonth.Text & "/" & _
eexpireyear.Text & "', payments.cardexpirationmonth='" & _
eexpmonth.Text & "', payments.cardexpirationyear='" & eexpireyear.Text & _
"', payments.cardaddress='" & eaddy.Text & "', payments.cardzipcode='" & _
ezip.Text & "', payments.threedigitnumber='" & ecvv.Text & _
"' where payments.filenumber='" & TextBox1.Text & "' and paymentstatus='PENDING';"
myconnstringtps = "server=localhost; user id=root; " & _
"password=1C0cac0la; database=collectionsmax"
Try
conntps.Open()
Try
mycommand.Connection = conntps
mycommand.CommandText = Updatepayments
mycommand.ExecuteNonQuery()
conntps.Close()
mycommand.Dispose()
Catch myerror As MySqlException
MsgBox("error connecting:" & myerror.Message)
End Try
Catch myerror As MySqlException
MsgBox("error connecting:" & myerror.Message)
Finally
If conntps.State <> ConnectionState.Closed Then conntps.Close()
MsgBox("Successfully Changed")
End Try
I am not getting any errors or exceptions when attempting to run the code.
I have tried to output the generated update query to a text box and running the code though mysql management studio, and it works perfectly. so im pretty sure its not an issue with the actual query being sent to the server.
I have used almost this exact same code to do insert into statements with no issues.
It is not updating the database when the code is ran through my VB.net application using the above outlined code.
You don't set the connection string in the MySqlConnection
myconnstringtps = "server=localhost; user id=root; password=1C0cac0la;......"
conntps = New MySqlConnection(myconnstringtps)
apart from that, you need to use parametrized query to avoid problems with single quotes inside your strings and the Sql Injection Attack security problem
Dim Updatepayments As String = "update payments " & _
"set payments.payorname=#name," & _
"payments.cardnumber=#cnum," & _
"payments.bankname=#bank," & _
"payments.checkaccountnumber=#actnum," & _
"payments.checkroutingnumber=#routing," & _
"payments.cardexpirationdate=#monthyear," & _
"payments.cardexpirationmonth=#month," & _
"payments.cardexpirationyear=#year," & _
"payments.cardaddress=#address," & _
"payments.cardzipcode=#zip," & _
"payments.threedigitnumber=#digits " & _
"where payments.filenumber=#file and paymentstatus='PENDING'"
Dim mycommand As New MySqlCommand(Updatepayments, conntps)
mycommand.Parameters.AddWithValue("#name", epayorname.Text)
mycommand.Parameters.AddWithValue("#cnum", eccnumber.Text)
mycommand.Parameters.AddWithValue("#bank", ebankname.Text)
mycommand.Parameters.AddWithValue("#actnum", eaccountnumber.Text);
mycommand.Parameters.AddWithValue("#routing", erouting.Text)
mycommand.Parameters.AddWithValue("#monthyear", eexpmonth.Text & "/" & eexpireyear.Text)
mycommand.Parameters.AddWithValue("#month", eexpmonth.Text)
mycommand.Parameters.AddWithValue("#year", eexpireyear.Text)
mycommand.Parameters.AddWithValue("#address", eaddy.Text)
mycommand.Parameters.AddWithValue("#zip", ezip.Text)
mycommand.Parameters.AddWithValue("#digits", ecvv.Text)
mycommand.Parameters.AddWithValue("#file", TextBox1.Text)
Other problematic point: Are you sure that your fields are all of string type? You pass for every field a string and surround the value with single quotes. This could fail if someone of your fields are not of string type. (these fields in particular could be not of string type payments.cardnumber, payments.checkaccountnumber, payments.cardexpirationmonth,payments.cardexpirationyear,payments.threedigitnumber)
Use command parameters. This makes it both safer (SQL injection) and easier to handle.
Dim Updatepayments As String = "UPDATE payments SET payments.payorname=#1, " & _
"payments.cardnumber=#2, ..." & _
"WHERE payments.filenumber=#11 AND paymentstatus='PENDING';"
mycommand.Parameters.AddWithValue("#1", epayorname.Text);
mycommand.Parameters.AddWithValue("#2", eccnumber.Text);
...
You can also use parameter names like #epayorname with SQL-Server but some connection types (like ODBC) only allow positional parameters.
Red alert You are obviously dealing with credit card information here and yet you are leaving yourself and your customers vulnerable to SQL injection attacks!
Also you have a password in your code that you posted on the public Internet!
(And Steve seems to have the right answer.)

VBA code works in Access 2007 but doesn't work in Access 2010. Any ideas?

My wife wrote the following code and it used to work fine for her when her organization used Access 2007. They just updated to Access 2010 and it no longer works. I am not familiar with Access at all but I suggested I'd present it to Stack to see if you guys can see anything straight off that won't work in Access 2010. Thanks in advance for any insights.
Private Sub Originating_Zone_AfterUpdate()
Dim EscortDB As DAO.Database
Dim rstBldgs As DAO.Recordset
Set EscortDB = CurrentDb()
Set rstBldgs = EscortDB.OpenRecordset("SELECT BuildingName FROM" & _
" ZoneBldgLookup WHERE ZoneLocation = '" & _
Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
"' ORDER BY BuildingName", [dbOpenDynaset])
rstBldgs.MoveLast
rstBldgs.MoveFirst
Do Until rstBldgs.EOF
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].AddItem rstBldgs!BuildingName
rstBldgs.MoveNext
Loop
rstBldgs.Close
End Sub
Update: She got it working using the following code. Thanks for your help!
Private Sub Originating_Zone_AfterUpdate()
Dim sBuildList As String
sBuildList = ("SELECT BuildingName FROM" & _
" ZoneBldgLookup WHERE ZoneLocation = '" & _
Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
"' ORDER BY BuildingName")
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].RowSource = sBuildList
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].Requery
End Sub
It's terrible code. Populating a dropdown list or listbox by walking a recordset and .AddItem is terribly inefficient. The whole thing can be done without code by simply assigning a SQL string to the Rowsource property of the combobox/listbox.
Now, clearly, the list changes based on the choices in the control to which this AfterUpdate event is attached, but all that means is that you assign the Rowsource in this event. Probably, all the above code can be replace with this:
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].Rowsource = "SELECT BuildingName FROM" & _
" ZoneBldgLookup WHERE ZoneLocation = '" & _
Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
"' ORDER BY BuildingName"
I can't say what's wrong with the code not working (I suspect there's a sandbox mode/macro security issue going on), but it's way more lines of code than are needed.
In addition to #David-W-Fenton's suggestions, I think you should use a string variable to hold the SELECT statement. Then you can Debug.Print it to the Immediate Window, copy it to a new query (in SQL View), and make sure it actually returns rows.
Dim strSql As String
strSql = "SELECT BuildingName FROM" & _
" ZoneBldgLookup WHERE ZoneLocation = '" & _
Forms!DateID!EscortIDSubform.Form.[Originating Zone] & _
"' ORDER BY BuildingName"
Debug.Print strSql
Forms!DateID!EscortIDSubform.Form.[Pick Up Location].Rowsource = strSql
Also if this is code in the module of a form named DateID, you can replace Forms!DateID with the keyword Me (which is shorthand for "this form" ... the form which contains the code you're running). That's not dramatically shorter, but Me will not need to be changed if the form is ever re-named. Still not a big deal ... just one less detail you won't have to fiddle with down the road.