I have searched for answers but the ones I have found I can't get working.
I have a database written in MS Access located on a network drive. The front-end has been written in Visual Studio 2010 (VB). I can get the data OK but I need to do a LIKE search to see if there is any records that might match what the user has entered.
Essentially the database is a list of barcodes provided on media. Users provide a list of barcodes but this is done by eye and the last two letters / numbers can be missed, due to size, location or not provided. When scanning the barcode it reads the full details. For example 'AIF00511L6'.
If there is a duplicate then I need to inform the user that there might be one or more records. Duplicate barcodes are possible (don't ask), so I need to provide the details to the user so that they can make the final decision as to if this is the same or different media.
The code I have in VB is:
Dim SQLString as String = "SELECT COUNT(*) AS Count1 " &
"FROM " & Table & " " &
"WHERE " & Column & " " &
"LIKE '*" & Search & "*';"
The code at runtime SQLString is:
SELECT COUNT(*) AS Count1 FROM Assets WHERE aItem LIKE '*IF00511L*';
This works OK in Access (returns 1) but it's not working from VS. I have tried using the '%' in place of '*' but this is not working either. In VS it returns 0.
Any help on this would be appreciated.
I saved and restarted my project and made the below change and now it's working!
Dim SQLString As String = "SELECT COUNT(*) AS Count1 " &
"FROM " & Table & " " &
"WHERE " & Column & " " &
"LIKE '%" & Search & "%';"
So it needed to be a % and not *, something I tried and didn't work, Oh well.
Related
I built an Access database to contain information regarding parts that we use to create schematics. There is one table that contain "basic" information, like "unique part ID" ("TUPID"), links to datasheets and so on - and the "Partition" further information is stored.
Furthermore there are several tables (this is "Partition") that contain information on the part itself: one table for resistors, one for connectors, one for power-ICs and so on. Each table has many fields different from other tables, but there are fields that exist on each table, eg. "Manufacturer", "Symbol", "Package" and "Height".
Now I have a split form ("10_Change_BaseInformation") that shows the "basic information", so when I select one row in the database-part of the form, the data is loaded into textboxes and can be edited. Additionally I want to see the information from the "Partition"-table in this form, so I wrote this:
Private Sub Form_Click()
Dim SelectedPartition As String: SelectedPartition = Forms![10_Change_BaseInformation]![Text25]
'Field "Text25" contains the TUPID
Dim SQLStatement As String
SQLStatement = "SELECT " & SelectedPartition & ".TUPID, " & SelectedPartition & ".[Mfg]" & vbCr & _
"FROM " & SelectedPartition & vbCr & _
"WHERE (((" & SelectedPartition & ".TUPID)=[Forms]![10_Change_BaseInformation]![TUPID]));"
DoCmd.RunSQL SQLStatement
' SQLStatement = "SELECT Resistor.TUPID, Resistor.[Hersteller] FROM ResistorWHERE (((Resistor.TUPID)=[Forms]![10_Change_BaseInformation]![TUPID]));"
End Sub
First of all, I get runtime-error "2342", but I can't make any sense of that; so: how do I have to modify my code, to get a valid result?
Second, how can I get the values from the query to the form?
Thanks in advance for your help!
I can't speak to the reason of your error, as we would need more information such as the types of your fields in your table.
I'm guessing that your Resistor.TUPID field is a string.
If so, you need to modify your code as below:
WHERE (((" & SelectedPartition & ".TUPID)='" & [Forms]![10_Change_BaseInformation]![TUPID] & "'));
If it is of Type number then
WHERE (((" & SelectedPartition & ".TUPID)=" & [Forms]![10_Change_BaseInformation]![TUPID] & "));
As to the form...
Generally speaking, you can design a form without a record source, but with controls that are named the same as your select query fields.
When designing each Control on the form, you have to explicitly specify the Control Source (i.e. The field names that are returned from your select query), otherwise they will be Unbound and never update automatically based on Record Source.
Then assign your SQL query string to the Recordsource of the form.
MyForm.Recordsource = MySQLString
Then run the Requery method of the form.
MyForm.Requery
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
I am making a database where the user can change the name of a company in a table. However , whenever I use the update query, it asks for a parameter which is already supplied. The old company name is in the variable new_comp and then the new one is in the Me.comp1_box.Value.
Funny enough the query runs excellently whenever I hit ok and enter nothing inside the "Enter Parameter" setting
Dim record_changer As String
record_changer = "UPDATE " & "[" & new_comp & "]" & " SET " & "[" & new_comp & "]" & ".Company_Name =" & """" & Me.comp1_box.Value & """" & ";"
MsgBox (record_changer)
DoCmd.RunSQL (record_changer)
This is the final value of the record_changer.
UPDATE [EREDEON TECHNOLOGIES] SET [EREDEON TECHNOLOGIES].Company_Name="EREDEON TECH";
This is how it is when the code runs.
This is the query that it is supposed to run
It gives this prompt meaning it's supposed to run perfectly, meaning there is nothing wrong with the Query
This what pops up
Can anyone please help me out?
I am genuinely lost.The name of the Old Company name is EREDEON TECHNOLOGIES and the new name is EREDEON TECH
But funny enough when I just hit Okay without entering a value into the parameter dialogue box, it actually makes the changes.-_- weird
This happens, then I press "OK"
THEN THIS HAPPENS, Then I hit Okay
This is the table before.
It updates the table the new value which is EREDEON TECH. When I just hit OK, without typing anything into Parameter Dialogue.
Try changing your SQL string to the following. Note the single quotes change around Me.comp1_box.Value.
record_changer = "UPDATE " & "[" & new_comp & "]" & " SET " & "[" & new_comp & "]" & ".Company_Name ='" & Me.comp1_box.Value & "';"
Misused quote marks is the most common cause for the Parameter Value prompt. If that doesn't work, use this article to perform step-by-step trouble shooting on all of the other usual causes, Why does Access want me to enter a parameter value?
You can also reference the following articles:
MS Docs, Quotation marks in string expressions
Bytes, (') and (") - Where and When to use them
Fundamentally, the issue is due to the use of double quotes in VBA which works in Access SQL by itself but not via VBA using DoCmd.RunSQL. You could have used single quotes to enclose company name value.
However, avoid concatenating VBA literals to SQL queries with quotes in the first place. Instead, use the industry best practice of parameterization which is available in MS Access using QueryDefs in VBA and PARAMETERS clause in SQL:
Dim record_changer As String
Dim qdef As QueryDef
sql = "PARAMETERS new_name_param TEXT; " _
& "UPDATE [" & new_comp & "] " _
& "SET Company_Name = new_name_param;"
' SET UP QUERYDEF
Set qdef = CurrentDb.CreateQueryDef("", sql)
' BIND PARAMS
qdef!new_name_param = Me.comp1_box.Value
' RUN ACTION
qdef.Execute
' RELEASE RESOURCE
Set qdef = Nothing
Nonetheless, the need to concatenate table name, new_comp, in query is questionable database design. Proper names (EREDEON TECHNOLOGIES, GAME DISCOUNT STORE, SHOPRITE, etc.) should never be names of tables. Avoid maintaining a separate table for every company. Instead, normalize data for a single table of all companies, then run UPDATE with WHERE adding a second parameter.
' PREPARED STATEMENT, NO VARIABLE CONCATENATION
sql = "PARAMETERS new_name_param TEXT, old_name_param TEXT; " _
& " UPDATE Companies " _
& " SET Company_Name = new_name_param" _
& " WHERE Company_Name = old_name_param;"
Set qdef = CurrentDb.CreateQueryDef("", sql)
qdef!new_name_param = Me.comp1_box.Value
qdef!old_name_param = new_comp
qdef.Execute
In fact, since above SQL is now separated from VBA, save the query without VBA punctuation (&, ", or _) as a separate object and call it in QueryDefs by name:
Set qdef = CurrentDb.QueryDefs("mySavedQuery")
qdef!new_name_param = Me.comp1_box.Value
qdef!old_name_param = new_comp
qdef.Execute
Even better, if your parameters derive from controls on open forms, include them directly in query for a single line VBA call. Below runs the normalized version of single Companies table:
SQL (save as query object, adjust names to actuals)
UPDATE Companies
SET Company_Name = Forms!myForm!comp1_box
WHERE Company_Name = Forms!myForm!new_comp
VBA (no need to close action queries)
DoCmd.OpenQuery "mySavedQuery"
So, I have an Access database where full names of patients are stored in a single field, ala "John Henry Michaels". I would like to be able get a name from the user search for it in the database. Now, here I found this
WHERE " " & tblEmployee.FullName & " " Like "* " & tblNames.Name & " *"
which is almost what I want. The only problem is that if the user doesn't know the middle name and enters "John Michaels", then "John Henry Michaels" will not show up. Is there a way to search the field for any part of the user input?
For this particular scenario I would separate the user input and put it in the query as 2 parts (You may need to set up a loop to separate the users input if there is a chance they may enter a name with more than 2 parts).
You would end up with a query that looks like this:
WHERE " & tblEmployee.FullName & " LIKE '* " & NamePt1 & " *' AND " & tblEmployee.FullName & " LIKE '* " & NamePt2 & " *'"
Alternatively, you may want to normalize your database and separate the name into 3 parts, as that is the standardized database design.