I'm building an access database, and on my main form, I have a field called "PartNumber." I want to scan data (a part number) into this field using a USB scanner and barcode. However, the barcodes include a leading "P" that isn't actually in the part number, and only serves to identify what kind of object that number is assigned to. I would like to automatically remove that P when I initially scan the data, so that the database stores and displays the correct part number.
I thought about using a query with an expression involving mid() and len(), then setting the control of the form field to that field in the query. However, that won't let me change or input data in the form's table, so it's useless for any records not already in the table.
Any ideas are greatly appreciated. Thanks!
I would do this with VBA, in the AfterUpdate event of the textbox.
Private Sub PartNumber_AfterUpdate()
If Left(Me!PartNumber, 1) = "P" Then
' Remove the first character
Me!PartNumber = Mid(Me!PartNumber, 2)
End If
End Sub
Depending on how the barcode API VBA runs: update tables via SQL or updates current form record in VBA; updates in bulk or updates one at a time. Either way at end of bar code reading when updating field, simply use Replace() in VBA or SQL:
SQL:
UPDATE tablename SET fieldname = Replace(fieldname, 'P', '')
VBA:
Me.fieldname = Replace(Me.fieldname, 'P', '')
Related
We have a field where users sometimes input incorrect info. It wasn't a problem until now when we tried exporting the info to an external program: the data is causing problems. So the idea is now to fix the rows that have the incorrect data. The problem is that the issue is not in all fields, so I am not sure how to adjust it without accidentally deleting valid info.
I want to remove the space and the "J" and the input after it (it always starts with "J"). I did a query with Left([EU-VAT NR], InStr([EU-VAT NR], "J") - 1), but the problem is that I still want the "SE" and "RO" rows that don't have the incorrect info.
RO 9999999 J11/1/1111
SE999999999
RO9999999
RO99999999 J11/11/1111
RO 99999999
What I've thought of is exporting to Excel and then use text-to-columns to split the info up and delete unwanted info to then paste the corrected info back into the table, but since it is a lot of rows it is impossible to detect if something goes wrong.
Create and use small helper function in your query:
Public Function TrimSpaceJ(ByVal Value As String) As String
TrimSpaceJ = Split(Value, " J")(0)
End Function
Assuming that is the only "J" in string, consider:
Trim(IIf(InStr([EU-VAT NR],"J")>0,Left([EU-VAT NR],InStr([EU-VAT NR],"J")-1),[EU-VAT NR]))
Or
Trim(IIf([EU-VAT NR] LIKE "*J*",Left([EU-VAT NR],InStr([EU-VAT NR],"J")-1),[EU-VAT NR]))
I'm currently setting up a label on Bartender. Currently I'm on Database setup screen and already have connected an excel sheet to it.
Inside an excel sheet, I have a column that is prefixed with a barcode number starting digits are '5011'. Inside an excel sheet I can select few records and drag down to generate more barcodes. e.g. if you have numbers going down a cell 1,2,3... and you select all three and drag down you have numbers following up with4,5,6,7,8 etc... same principle with my barcodes.
But.. the '5011' needs to start with a '0', so it becomes '05011'. If I format my barcodes with an '0' and drag down, cells do not update to what they should to. e.g. 01,02,03.. will continue like this: 03,03,03,03,03 repetitively.. even when refreshing the workbook with F9
I need an SQL query that just simply adds an extra '0' at the start for the barcode column and shows all results within the sheet.
I have managed to use this query to run on excel sheet:
`SELECT * FROM [Sheet1$];
just not sure how to add that extra 0 for column named Outer Barcodes
and have tried this:
UPDATE `Sheet1$` SET `Outer Barcode` = Concat('0',`Outer Barcode`)
but an error message comes up with:
Undefined function 'Concat' in expression.
The concat() operator in MS Access is &:
UPDATE [Sheet1$]
SET [Outer Barcode] = '0' & [Outer Barcode];
Hi I am really new to VBA Macros and would appreciate any help on this matter.
I need to write a macro to change the value of a column to 'checked' based on different search criterias. My requirement is that one of the columns values must be automatically marked as checked based on search criterias which will search for existence of serial nos contained in a string in one column inside string values of other columns.
Please help me in creating a macro to accomplish this task.
I am sorry to say that I cannot share the code or attach a screenshot to further explain the scenario because of confidentiality reasons.
Detailed explaination
Column to be modified : Comments (initially blank)
Columns used for search criteria : Can be any column in the existing table.
Search criteria:
A substring from the reference column (basically a number with 6 digits will be selected as the search key)
Note : There is no specific format followed by this column (and we can do nothing about that) eg: In one column the entry will be 35567890-DEF-GHJ while in another it will be like Ref:35567890-- and in another column field it will be like CEK 35567890.
The substring must be checked not only for the entries in the same coulmn but with the contents of the entire table. Basically it is like the find function in the excel.
If a match is found I need to add up the values in the debit and credit entries and see whether the result is 0. If the result is zero, I need to enter 'checked' in the comments field that allows string values.
This is not one complete macro but still does the work.
Macros where used only for extracting the product code. For the remaining processes I used existing functions as in, performed a comparing function on the net amount and then used a simple if to mark the column as check.
Macro used for extracting product code.
Public Function getProductCode(source As String) As String
Dim rExp As Object, allMatches As Object, match As Object
Dim result As String: result = ""
Set rExp = CreateObject("vbscript.regexp")
With rExp
.Global = True
.MultiLine = False
.Pattern = "(?:^|\D)(\d{7,8})(?!\d)"
End With
Set allMatches = rExp.Execute(source)
For Each match In allMatches
result = result + match.SubMatches.Item(0)
Next
getProductCode = result
End Function
A new column 'Reference' was then created on which the macro was used passing the specific column and the extracted product code is stored here.
After this a function was used to find duplicate entries and sum up the total number of products sold. The formula I used is as follows:
=ROUND(SUMIFS([Total],[Reference],[#Reference]),2)=0
Finally I used a simple if to mark the column as checked if the previously generated result (in column status) is true.
=IF([#Column1]=TRUE, "Checked", "")
This is how I got it done.
There might be better way to do it. But if anyone else is facing the same problem this will help.
G'day, everyone.
I've been banging my head against this question the whole day through today, and I haven't managed to find any answers, so I'd appreciate your help.
What I have:
An Access form containing a text field
A query which is the form's data source
A custom function called RegExp defined within a module
RegExp takes two values as input: string (obtained from a table) and pattern (obtained from the form). RegExp returns a boolean value which in turn thins out query results.
The function works perfectly fine and as expected, however, this is only the case when the user fills out the text field. If the field is left blank, no results are returned (and the function's not even getting called if that's the case).
So here's what that particular statement within the query looks like:
... AND (RegExp(tblRole.Description,Trim([Forms]![frmFindRole]![txtRegExp]))<>False) AND ...
(Basically, to sum it up, user types in a value into the text field which gets leading and trailing spaces trimmed off, converted to a regular expression inside a VBA module, and then query results get filtered based on what boolean value the function returns).
There is a number of controls on this form, and they worked prior to me adding that txtRegExp text field. Now the query only returns results if txtRegExp is filled out, and I have no idea why. I've tried adding more checks, but the query's too complicated already, and I haven't succeeded.
If additional code samples are required for an answer to be made, I'll be able to provide them tomorrow.
Thank you in advance.
P.S. Would Nz help? If yes, then how would I go about using it?
Based on the few explanations you gave in comments
Suppose that this is code triggered on the KeyUp event :
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Me.Requery
End Sub
Store the default SQL for your form's recordsource somewhere in a local variable. In this example I considered that you stored it in SQLdefault string.
Prior to requery, check if the textbox is empty and if yes change your form's recordsource SQL accordingly:
private SQLdefault as string
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim SQL As String
If Nz(txtRegExp, "") = "" Then
SQL = SQLdefault
SQL = Replace(SQL, "AND (RegExp(tblRole.Description,Trim([Forms]![frmFindRole]![txtRegExp]))<>False)", "")
Me.RecordSource = SQL ' Normally this is enought to requery, if not uncomment below
'Me.Requery
Else
Me.RecordSource = SQLdefault ' Normally this is enought to requery, if not uncomment below
' Me.Requery
End If
End Sub
In this example I just remove the SQL part containning :
AND (RegExp(tblRole.Description,Trim([Forms]![frmFindRole]![txtRegExp]))<>False)
Replace it by something else if that's not correct.
That's obviously not the most elegant solution but it's difficult to provide with the best solution with what you've shown.
I've managed to make it work by modifying my initial query to include a check for the value of txtRegExp.
I am still not entirely sure why it failed with a blank txtRegExp value. I have a feeling the RegExp function somehow didn't fire when provided with NULL as the second parameter.
I am very grateful to Thomas G for all the help he's provided.
I have a subform with user input (text box), that can reach more than 255 chars and can have special characters. As I cannot assign it as a parameter to a vba query definition (only 255 characters will work), I thought about directly referencing the text box:
Insert into ...
Values (Forms!MainFormName!txt_MyTextField, ... )
does work in a query in the Access GUI, but not in a vba query definition specified with the following code:
Set query_definition = CurrentDb.CreateQueryDef("", SQLQuery))
"Forms!MainFormName!NameOfSubformNavigationElement!txt_MyTextField" does not work either as a reference, just in case you are wondering. Neither does "Forms!MainFormName!NameOfSubformNavigationElement.Form.txt_MyTextField"
How can I get what I want? The string can have any kind of special chars like ' and ". Is there no alternative than appending the user input into the sql query? Is there some built-in function to escape these values?
UPDATE: Added another example now that the environment is known.
To use the form field in an action query, create a public function that will return the desired field, then call that function from your query.
Public Function GetFormField() As String
GetFormField = Forms![MyMainForm]!MySub.Form.MyField
End Function
In your query, use something like:
WHERE ((([106thZip_code_database].zipcode)=GetFormField()));
The following is an example you could use in the Main form to reference a field in a subform. 'MySub' is the name of the control on the Main form - not the subform's object name.
Debug.Print Forms![MyMainForm]!MySub.Form.MyField
A good reference on this subject can be found at: http://access.mvps.org/access/forms/frm0031.htm