Alter Large Block of Records with Data Macro - ms-access

Several hundred records were incorrectly entered into a table in my Access Database.
A particular ID number includes a two digit leading code that designates the state that record is from; so 01-24586 is a record from the ACT, and 02-55719 is a record from NSW. The incorrect entries have these two switched. I need to replace the first two digits of these records' IDs with the correct code.
To do this, I've tried to write a Named Data Macro that I can call from a regular macro object (so I can double click it in the navigation pane). I've done that, but it doesn't seem to work. My Data Macro (just one of the State fixes) looks like this:
If [State]="NSW" Then
For Each Record In tblCustomer
Where Condition =[State]="NSW"
Alias NSWCust
EditRecord
Alias NSWCust
SetField
Name MyobID
Value = "02-" & Right([MyobID],5)
End EditRecord
End If
When I call it from the other macro, using RunDataMacro it gives me error 3709.
Is this a bad way to go about fixing this? What's wrong with my execution?

Data macros are intended to be used as "triggers" (perform an action when a specific event occurs.
In order to update data, you should use an update query.
Statement would look like this:
UPDATE tblCustomer
SET MyobID = "02-" & Right([MyobID],5)
WHERE [State] = "NSW"

Related

How do you retrieve the result of a DCount expression from a related textbox for multiple records in a continuous form in MS Access?

Hello I have been trying to return the result of a DCount within an MS Access form. I have a field called "Process" which stores a process such as "Shipping" and Sub-Process field which stores text too e.g. "Engineering". I have another field which needs to store the count of how many shipping processes have a specific sub-process such as answering "how many sub-processes are there for each process?" called txtcountsubprocesses.
The related text field is saved as txtprocess and the count field (the one I can't seem to get right) has the name txtcountsubprocesses. In the Control Source property for txtcountsubprocesses I have the following expression:
=DCount([Sub Process],[LogisticsData],[Process]=txtprocess.text)
I receive the following error message: #Name?
I have tried multiple other ways of programming the DCount however the way mentioned above seems to be the closest I have to a possible answer. I have also checked the spelling of the table headers from [LogisticsData].
Is it possible to solve my problem using a DCount method like this?
Required output (sorry not sure how to put in a proper table):
Process ; Sub-process ; Sub-processCount (3 columns)
Shipping ; Engineering ; 5 (three pieces of related data - the number being what I need to produce)
Shipping ; Medical ; 4 (three pieces of related data - the number being what I need to produce)
Firstly, DCount needs string parameters to identify field and table name.
Secondly, if you want to include something from the current form, you can concatenate those strings, or use a form parameter.
Thirdly, you should almost never use .Text. Use .Value instead. You can only use .Text on controls that have focus.
So, with all these things fixed:
=DCount("[Sub Process]","[LogisticsData]","[Process]='" & txtprocess.Value & "'")
You might want to read into using parameters, since oddities might occur when the value is Null or contains single quotes

Microsoft Access 2010 filtering data based on tempvar

i have a web database and im trying to filter a datasheet, based on the contents of a tempvar. Im trying to use the record source property of the datasheet to do this.
I need to do this because, every employee that logs in should only be able to see a given subset of data in the products table. In the employee table, i have an extra column with a string value which is the data that particular employee should see.
I have a login form that on clicking login, adds this string to the tempvars collection.I can see the tempvar has been added in the immediate window as shown below:
?tempvars!tmpgrpdsc -> "IAMS"
i use the query builder option to complete the record source property as shown below.
The problem is, nothing is returned !
But when i enter the string "IAMS", i get records returned.
However, i have done this with another datasheet and it has worked, the tempvar here held a number ! See below:
What am i missing or is there a better way to filter records based on the login. Thanks
What you showed should work.
However, have you tried to change the criteria to ="""" & [Tempvars]![tmpGrdsc] & """"
Also, to make sure that your tempvar is actually containing the data during the query, you could show it as a field, just to check exactly what data is being returned during the query:
SELECT Orders.*,
[Tempvars]![tmpGrdsc] AS TmpGrdsc
FROM Orders

Access 2010 VBA Form Pulling In Table Data

I have an "Invoices" table, which I run an "Invoices From off of to create invoices for my clients. All my client data; address, hourly rate, etc. is on my "Client Lists" table. The Tables are linked together by a common account/client ID.
I don't want to update general items like hourly rate, address, every time I create an invoice. I am looking to set up an after update event that will auto populate these general items from my "Client Lists" table when I enter the client ID on the "Invoices" form.
---Update---
Below is the code I am working with, and I keep getting an error "Expected: list separator or).
In normal speak; The service type field on my "Invoices" form, should look at my Service type field, on my Client Lists table, for the same Client ID as Account ID.
Private Sub Client_ID_Change()
Service_Type = DLookup("Service_Type", "Client Lists", "Account ID=" & Client ID)
End Sub
Have you tried anything?
The short answer is:
create a query to represent the data you want. In vba use a recordset to run the query's logic for the ID you've got, then display the returned data in the recordset on your form.
However there's no detail here, as you have little yourself
Syntax error on the dlookup function.
To specify a search criteria (last parameter of dlookup) based on a field that contains a blank space, you will have to enclose it in brackets. Also consider that control names can have blank spaces, but to reference them in vba you must replace write them using underscores.
"[Account ID]=" & Client_ID
I would strongly suggest you name table fields and form controls without blank spaces.

Howto Validate External Data Source before Importing in MS Access

I have an MS Access Database that uses an external text file for input.
I don't trust this text file and I want to validate the contents before I import the data to avoid contaminating my database.
The data is imported from the text file via a Macro which is working perfectly fine on its own. I am trying to wrap the query that does the insert inside an "If" Program Flow block in the macro. I am having trouble getting the condition in the If statement to work.
The external text file source is known as the table UserStatistics
I have created a query (UserStatistics-CheckTxtFileIsCorrect) that contains the following code:
SELECT *
FROM UserStatistics
WHERE (((UserStatistics.[Column1])="KNOWNGOODVALUE"));
The If condition in the Macro is currently set to:
count (*) from [UserStatistics-CheckTxtFileIsCorrect] >1 but this errors out.
Everything I've tried fails with the error "cannot parse..." or "cannot find the name you entered..."
Any help would be greatly appreciated!!
UPDATED with list of variations I've tried:
Count([UserStatistics-CheckTxtFileIsCorrect])>1
-- "Access cannot find the name 'UserStatistics'
-- you entered in the expression"
Count[UserStatistics-CheckTxtFileIsCorrect] > 1 -- cannot parse
count (*) [UserStatistics] >1 -- "cannot parse..."
Count *
where [UserStatistics-CheckTxtFileIsCorrect]![User ID] = 'ABC' -- cannot parse
Count(select * from [UserStatistics]
where [UserStatistics]![Column1] = 'ABC') > 1 -- cannot parse
MAJOR UPDATE 2
HansUp suggested DCount. If I omit the criterion part of the expression the If condition is now evaluating. But the criterion part is definitely needed to acheive my goal.
DCount("*","UserStatistics","[UserStatistics]![Column1] = 'ABC' ")>1
DCount("*","UserStatistics","Column1 = 'ABC' ")>1
DCount("*","UserStatistics",Column1 = 'ABC' )>1
All of the above give an error 2001
Solution!
It turns out that my external text file column names contained spaces. So the column in the criterion of the DCount statement needed to be wrapped in square brackets like so:
If DCount("*","UserStatistics","[User ID]='KNOWNGOODVALUE'")>1 Then
Do my Actions here....
Else
MsgBox Error here...
End If
Many Thanks to HansUp for pointing me towards DCount.
Use a DCount() expression in the condition.
DCount("*", "UserStatistics", "Column1='KNOWNGOODVALUE'")>1
That should return the same count as your query, but you don't actually need the query because you can include the source table name and the WHERE criterion as DCount options.
You can test that DCount expression in the Immediate window to work out the third option. (Ctrl+g will open the Immediate window) Try the following line there:
? DCount("*", "UserStatistics", "Column1='KNOWNGOODVALUE'")
Mind the quotes ... there is a set of single quotes in a string which is enclosed by double quotes.
Since the third option, the WHERE criterion, is the obstacle, use the query with DCount and omit the third option.
DCount("*", "[UserStatistics-CheckTxtFileIsCorrect]")

Ms access: Autocomplete field with values from another table

please forgive me for my poor english and my big ignorance on programming.
I'm using Ms Access 2003.
Let's suppose i have two tables:
Table1: ID (autonumber), [...], Keywords (memo)
Table2: ID (autonumber), Keyword (text)
I want:
1) As the user types letters in Table1.Keywords that my database searches in Table2.keyword for the nearest value and proposes it by autocompleting (just like google proposes a search word as you type)
2) When user presses ", " that he can add one more keyword in the same field (and the autocomplete still runs for this next value)
3) If he types a keyword not included in Table2 and press ", " that he is asked if he wants this value to be added in Table2
Well, i'm not sure if all these are clear... maybe they are a lot of things...
But i'd appreciate if you could help me...
Thanks in advance
J.
It would be complicated to do it with a single control, but with two controls, a dropdown list for choosing the value to add, and a textbox displaying the memo field, you could have the combo box's AfterUpdate event append a comma and the chosen value to the existing data. Something like this:
Private Sub cmbChooseKeyword_AfterUpdate()
If Not IsNull(me!cmbChooseKeyword) Then
Me!txtKeywordMemo = (Me!txtKeywordMemo + ", ") & Me!cmbChooseKeyword
End If
End Sub
You'd also want the rowsource of your combo box to not list items that are already entered, so this is one way that would work for a relatively short list of keywords:
SELECT tblKeywords.*
FROM tblKeywords
WHERE InStr(Forms!MyForm!txtKeywordMemo, tblKeywords.Keyword) = 0;
Then you'd add:
Me.Dirty = False
Me!cmbChooseKeyword.Requery
...at the end of the AfterUpdate code above (inside the End If):
Private Sub cmbChooseKeyword_AfterUpdate()
If Not IsNull(me!cmbChooseKeyword) Then
Me!txtKeywordMemo = (Me!txtKeywordMemo + ", ") & Me!cmbChooseKeyword
Me.Dirty = False
Me!cmbChooseKeyword.Requery
End If
End Sub
...and you'd want to add the requery to the OnCurrent event of your form, as well (so that when you arrive on a record, the combo box already omits any keywords that are already in the list).
Now, all that said, I'd completely recommend against doing this. This is a denormalized way to store the data, and this leads to problems:
what if you want to delete one keyword?
what if you want the keywords to be sorted in alphabeticsal order?
what if you have 100s of thousands of records and you want to search this field with LIKE "*Keyword*" -- will it bog down to be terribly slow (no indexes, and not used well even if there were)?
You really should use a proper many-to-many structure, with an additional table between the one where you're currently storing the keyword memo and your keyword list table. This "joins" the two, and would then give you a list.
You could then use a subform with a dropdown list to populate each row of the join table.
If you like presenting the keywords on reports as a comma-separated list (as you're currently storing them), you can write a simple function to do the concatenation for you at the presentation layer of your reports (concatenation functions for that purpose are a frequent Access question here on Stackoverflow).
Why not use a "Combo Box" and set its Row Source Type to Table/Query, and then make the Row Source a query on the second table. Just make sure you don't turn on Limit to List.
CodeSlave mentions a way that will work. But it will only work for one value. There is no way to do the multi-words-separated-by-commas thing. Only one word at a time.
As for the Adding new values. The combobox control support an OnNotInList event which can do what you say.
Seth