Good morning people, or rather midnight.
I am totally new (again) to programming since I had to drop college for years due a lack of economic power and started fresh over,hence after several years I have forgotten almost everything.
Here is the thing though, I never thought I would have problem with a simple insert into. I am using visual basic and I am trying to save the following sentence in a database
Dim insertQuery As String = "INSERT INTO Camion (Plate, BrandID , ModelID, Year, Engine, Chasis, WeightLimit ,DateofPurchase) VALUES ('" & txtplate.Text & "','" & cmbBrand.Text & "','" & cmbModel.Text & "','" & Year.Value & "','" & txtEngine.Text & "','" & txtChasis.Text & "','" & nudWeightLimit.Value & "','" & dtpDateofPurchase.Value & "')"
ExecuteQuery(insertQuery)
MessageBox.Show("Info succesfully saved")
Now here is the issue. My combo box shows the name of the items instead of the code which is ok, I want it to do this so that people know what they are selecting. However, at the time of saving, this goes into a table where it only saves the ID of the combo box (not where it draws the data but a 2nd table where it stores all this info) but the sentence I am showing tries to save the Text displayed in the combo box in a int type of field in the database.
I am using mysql. So that is basically my issue, when I try to save the combo box of let's say brand , on the display list it shows Nissan and Mercedes. (in the database where it pulls that info, nissan is id 1, and mercedes is id 2). So it shows the name on the list as intended so people knows what they are selecting, but when I try to save it. I end up saving the name in a INT field, which shouldn't happen, is there a way to get the ID from the combobox and save that in the database?
Sorry if the question seems terribly silly as I am sure there is a easy solution but I haven't found one, even with google.
Related
I am trying to build this query through VBA instead of building it in Access and running a docmd.openquery. That seemed to me like the easier route, but I wanted to work on my SQL. Obviously that didn't work as intended if I am here lol.
So, I am trying to take the Date values of 14 text boxes on our JobTicket form and insert them into another table, Tbl_Schedule. This table is not a part of the Query that is the record source for the JobTicket form. I am worried that attempting to add this table in will overload the Query, as it is already very full. When I try to quickly navigate to the last field in that Query the text writes on top of itself, and then Access goes not responding while it clears up the text and loads the last couple fields. Adding another 56 fields to that seems like a recipe for disaster. I will post the SQL I have written below.
DoCmd.RunSQL "INSERT INTO Tbl_Schedule (Date_Scheduled1, Date_Scheduled2, Date_Scheduled3, Date_Scheduled4, Date_Scheduled5, Date_Scheduled6, Date_Scheduled7, " & _
"(Date_Scheduled8, Date_Scheduled9, Date_Scheduled10, Date_Scheduled11, Date_Scheduled12, Date_Scheduled13, Date_Scheduled14)" & _
"VALUES (#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled1_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled2_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled3_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled4_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled5_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled6_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled7_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled8_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled9_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled10_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled11_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled12_JobTicket] & "#, " & _
"(#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled13_JobTicket] & "#,#" & [Forms]![Frm_JobTicket]![Txt_DateScheduled14_JobTicket] & "#)"
Table being inserted into: Tbl_Schedule
Fields being inserted into: Date_Scheduled1 -to- Date_Scheduled14
Getting data from text boxes: Txt_DateScheduled1_JobTicket -to- Txt_DateScheduled14_JobTicket on Frm_JobTicket
Any other questions that would assist you in assisting me please feel free to ask! Thanks in advance!
Dynamic SQL has its uses, but this is not one of them.
Using DAO methods makes your code so much simpler and easier to read and debug.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim frm As Access.Form
' for readability
Set frm = Forms!Frm_JobTicket
' open table for adding record(s)
Set db = CurrentDb
Set rs = db.OpenRecordset("Tbl_Schedule", dbOpenDynaset, dbAppendOnly)
rs.AddNew
rs!Date_Scheduled1 = frm!Txt_DateScheduled1_JobTicket
rs!Date_Scheduled2 = frm!Txt_DateScheduled2_JobTicket
' etc.
rs.Update
rs.Close
With enumerated field names like these, you can also use a loop:
Dim i As Long
rs.AddNew
For i = 1 To 14
rs("Date_Scheduled" & i) = frm("Txt_DateScheduled" & i & "_JobTicket")
Next i
rs.Update
This is a good opportunity to consider normalizing your data so that part of your problem is removed entirely. Instead of having DateScheduled1_JobTicket, DateScheduled2_JobTicket etc., it might be better to have another table which fills vertically instead of horizontally, perhaps with fields like ID, Item, JobTicketNumber, ScheduledDate.
Then, fill this table with a row for each item/sku/product, and date. You'll have 14 rows for scheduled tickets for each item/sku/product instead of 14 columns, and this will also solve your future problem of adding 56 fields. The benefit is that you can present the job ticket schedule rows by using continuous forms (in a list). Even better, you can put this continuous form with dates as a subform on your item/sku/product main form, which will then show as a neat list of scheduled tickets that will automatically change as you scroll through item/sku/products.
If you don't use continuous forms, you can still use an unbound approach as you're using now. One benefit is that it will be much easier when you need to add future JobTicket numbers, since you can just add more rows instead of adding fields and having to do additional design work.
If you want to view data in the flattened way that you built your table, you can use a Crosstab query to present it as you have in your table, but the underlying data will be much better stored in a normalized format.
Note that you don't need to concatenate a string as you did above; just leave the Forms!Form!Control reference expression directly in the query and you have a nice parameterized query that will execute just fine, so long as there are dates in those controls (text box, drop down etc).
ex.
Insert Into (MyDateField) Values (Forms!MyForm!MyDateControl);
No dynamic SQL needed.
Trying to use a form named 'Customer Entry', that when clicked, enters the data that has been typed in said form into a table that is named 'CustomerRecord'. I keep getting errors and am at my wits end. Here is my code below, this is in VBA.
Public Sub Command19_Click()
CurrentDb.Execute "INSERT INTO CustomerRecord(Customer Name, APM, UAID, Context Code, Purpose Code, Context Description, Purpose Description) " & _
" VALUES (" & Me.txtCustomer Name & ", " & Me.txtAPM & ", " & Me.txtUAID & ", " & Me.txtContextCode & "," & Me.txtPurposeCode & ", " & Me.txtContext Description & ", " & Me.txtPurpose Description & ")"
frmCustomer
Entry.Form.Requery
End Sub
If you want to edit data from two tables on a single form you need to make an updatable query to base your form upon. Set your forms RecordSource property to be the updatable query. Now you can add form elements from the source that allow the user to edit all the fields directly.
See this list of pitfalls to ensure that your query is updatable:
http://allenbrowne.com/ser-61.html
If you absolutely must edit data elsewhere from your form, which you occasionally must do, don't use an SQL query execute statement to do so. Use a recordset object instead. This is both more secure, more reliable, and easier to read the code.
See this guide for an example of how it's done: https://learn.microsoft.com/en-us/office/vba/access/Concepts/Data-Access-Objects/modify-an-existing-record-in-a-dao-recordset
Additional reading: https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/recordset-edit-method-dao
Getting this error when trying to add a new group using the information entered by the user.
Microsoft Access Database Engine error '80040e14'
Syntax error in INSERT INTO statement.
/student/s0190204/wip/group-add.asp, line 79
This is the sql, the session is getting the ID of the user logged in.
sql_comd="INSERT INTO group (grp_USERID, grp_name, grp_caption, grp_content, grp_DATECREATED, grp_OPEN) VALUES('" &_
session("usr_ID") & "','" & _
request("grp_name") & "','" & _
request("grp_caption") & "','" & _
request("grp_content") & "','" & _
date & "','" & _
request("grp_open") & "')"
Thanks
I suggest you trace this through, and post the output string that you're passing directly to SQL. Just put a breakpoint in there right as the string is created and check it out.
There's not much wrong with this code, persay, but you haven't made sure that your input strings are clean. It's probable that they have a quotation mark in them or the date isn't in the proper string format and so on.
Additionally, note that this query is highly susceptible to query injection because it seems to stuff input directly from the request through to the database.
OH, and. Your 'Insert INTO' needs Group in []. (like [Group]) And your dates should probably be DateTimes instead of strings. (if this works with just the brackets) See my note on your other question. (And avoid using these (http://technet.microsoft.com/en-us/library/ms189822.aspx) in your schema)
And you can set the 'default' of the date to '=GetDate()' instead of passing it through from the C# side, which I personally prefer.
Okay, so what i"m doing is creating a word file with my application with my labels and textboxes to create a receipt. In my word file, I am trying to write what is in a first name label beside what is in a last name label.
Here is the code I have, but I just seem to be missing something, because the word file keeps putting a huge gap between the first name and last name. I'm sure it's a very simple fix, but Google doesn't seem to be helping with what I am trying to describe.
printFile.WriteLine("Name: " & vbTab & FirstNameTextBox.Text + LastNameTextBox.Text)
I assume it must be the vbTab keyword.
which is fixed and cannot be changed.
That said you can manage a workaround as follows:
Public Const MyTab = " "
and in your code
printFile.WriteLine("Name: " & MyTab & FirstNameTextBox.Text + LastNameTextBox.Text)
Need help using vba to count button clicks. any suggestions???
couner = counter+1
this is what i found.
"The form does not necessarily have to have any link to the Audit Table, use a recordset to update the counter value. Use the DLookup function to find out the value in the Table ie
DLookup "[Counter]","Audit"
A progression from this would be to have an AuditTable with [Autonumber] and [ButtonName]. In this way you could count several ButtonClicks but give each button in your Db a separate name so to count these
DCount "[AutoNumber","AuditTable","[ButtonName] = 'NameOfButtonToCount'" You can then put this alongside the button in a visible or hidden field."
Actually this is for an excel doc. I have to submit a spreadsheet for pc printer mapping to another group to process. the purpose of counting clicks is to help tell the program to go to the next row in excel. I add anywhere between 1-15 pcs to one spreadsheet and just trying to automate the process.
Going to assume you want some sort of audit log for actions taken using buttons on an Access form. Without knowing anything else all I can do is try to point you in the right direction. If you're trying to see who is doing what and when in your database then you can create an AuditTable with fields: Form, Button, Timestamp, User, Computer. Whenever the user clicks a button, executes a SQL statement to update the audit table:
Dim SQL As String
SQL = "INSERT INTO " & _
"AuditTable " & _
"(Form, Button, Timestamp, User, Computer)" & _
"VALUES" & _
"('" & Me.Name & "','" & YOUR_BUTTON_NAME_HERE & "',#" & Now & "#, '" & Environ$("username") & "','" & Environ$("computername") & "');"
CurrentDb.Execute SQL
If you want to count your button clicks you can then: SELECT Button, count(1) as Clicks FROM AuditTable GROUP BY Button