Howto Validate External Data Source before Importing in MS Access - 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]")

Related

Sum Values not equal to a space from a Control Source in MS Access

As the subject expresses, I'm trying to sum the values of a string field where spaces may exist. It must be done this way, unfortunately.
The database is very old. The original developer chose to make all fields Text fields; to get over the null value problems, a function was written in VB6 to replace any null value with a space. This cannot be changed.
Fast forward to now, I'm trying to create a report that sums the length field without changing spaces to nulls first, and it should be done entirely through the control source property of the report.
I've added some of what I've tried below, but every time the report is run, I receive:
Data Type Mismatch
...and I'm not sure how to get around it.
Ideally, I'd like to keep the users out of the database completely, and just add a combo box that lists the reports created in the database so they can be opened by name without having to run any additional update queries first.
=Sum(IIf([MY_LEN]<>" ",DCount("[MY_LEN]","MY_TABLE"),0))
=Sum(Nz(Iif(Trim([MY_LEN])='',Null,[MY_LEN]),0))
=DSum("[MY_LEN]","[MY_TABLE]","[MY_LEN]<>' '")
=Sum(Iif(Val([MY_LEN])>0,[MY_LEN],0))
=(SELECT Sum([MY_LEN]) AS MyLen FROM MY_TABLE WHERE (((MY_TABLE.[MY_LEN])<>' ')))
Is this possible?
Can't compare anything to Null. Can't say If x = Null Then because Null is undefined. So you can't test if undefined = undefined. Use If IsNull(x) Then in VBA and Is Null in query criteria. Don't really need IIf() for Sum() aggregate, other aggregates such as Count or Avg would.
To handle possible space, empty string, or Null for a text field holding numeric data.
=Sum(Val([MY_LEN] & ""))

Alter Large Block of Records with Data Macro

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"

MS Access 2010 query using tempvar inside IN() condition

I am trying to filter a query using a temporary variable that is inside an IN() condition. The temporary variable contains a list of text values. I am using the macro-builder tool in Access 2010.
Assume I have a query qryMain that produces:
Field1 Field2
1 A
4 B
2 C
3 D
without a WHERE clause. Using the clause
WHERE [tblMain].[Field2] IN([TempVars]![tmpField2])
to filter the query, the desired results are
Field1 Field2
1 A
4 B
when tmpField2 is set to "A,B". I set tmpField2 using an on-click event in form frmMain using SetTempVar and Requery the subform/subreport object sfrmMain that is based on qryMain. This is done via the MS macro-builder, not VBA.
Unfortunately, requerying sfrmMain produces an empty table rather than the expected results. Note that if I set tmpField2 to "A", then the requery macro works as expected.
I have tried multiple variations of initializing tmpField2, based on Access's double quote requirements, but still no success. My question is similar to this as-yet unanswered question, but my question involves passing the temp variable inside an IN() statement within the WHERE clause, without using VBA.
The problem is not actually due to TempVars. If your value list came from a form's text box instead of TempVars, the result would be the same.
For what you're attempting to do, IN () requires a hard-coded list of values:
SELECT m.Field1, m.Field2
FROM tblMain AS m
WHERE m.Field2 IN ('A','B');
But, instead of a hard-coded list of values, you want to supply the list dynamically when the query is run:
WHERE m.Field2 IN (something_dynamic);
Unfortunately, Access will not cooperate. Whatever you supply for something_dynamic, Access will interpret it to be only one value ... not a list of values. And it doesn't matter what method you use to supply something_dynamic ... a TempVar, a text box, a formal query parameter, a custom VBA function which returns a string containing a list of values ... that list will be evaluated as only a single value.
If you were willing to use VBA, you could write the query at runtime to include a hard-coded value list before executing it. Since you want to avoid VBA, you can try something like this ...
WHERE InStr(1, [TempVars]![tmpField2], "'" & m.Field2 & "'") > 0
Note that approach requires quoting text values within tmpField2: 'A','B'
Also beware that approach could be painfully slow with a large table. Access would need to evaluate that InStr expression for every row in the table.

Microsoft Access Queries Too Many Fields

I am dealing with approximately one dozen MS access tables that are all linked into a single database file.
Occasionally I would like to be able to get the union of these tables to generate a complete dataset. My attempts to do this so far result in a too many fields defined error.
Is there a way for me to do this using Microsoft access (I can use 2007 or 2010 versions)? If not, any other suggestions on how this could be achieved would be greatly appreciated.
According to the "QUERY" section of the "Access 2010 Specifications" topic in Access help, you are limited to 255 fields (columns) per recordset (query). If the "complete dataset" you want to produce will have more than 255 columns then you won't be able to do that in a single Access query.
One possibility might be to create several queries with no more than 255 columns each, export them to Excel, then paste them together side-by-side into a very wide Excel document. Excel_2010 worksheets can have up to 16,384 columns. It could be a bit tedious to do manually but there would be some opportunity to automate the process, at least in part. You'd also have to be careful that each "partial query" returned the same number of rows in the same order so things would "line up" when you pasted them together.
This answer is about a different occurrence of the "Too many fields defined" error from a query, and what I did to eliminate it.
I have a query that was giving me that error. Compacting the database didn't fix it. When I split the query into three queries with fewer fields in each, two of those smaller queries worked okay, but one still gave me the error. So it appeared that the problem had nothing to do with the number of fields, but with something about the coding of the query.
The query had two similar fields coded as subqueries:
(SELECT first([Date]) FROM [C:<path>\sources.accdb].[items - subjects] WHERE [Subject name] = SubjName and Assessment like "*Buy*") AS FirstBuyRecDate
and
(SELECT first([Date]) FROM [C:<path>\sources.accdb].[items - subjects] WHERE [Subject name] = SubjName and Assessment like "*Sell*") AS FirstSellRecDate
The error first arose when the second of those fields was added to the query. The query was able to work with either of those fields present, but not both. It was also able to work if the subquery in either of the fields was replaced with a random date constant, such as #2018 12 31#. So it appeared that the problem had something to do with those subqueries.
I was able to eliminate the error by changing those subqueries to function calls using a UDF I had previously developed for another purpose:
Public Function vFtnInQry(sField As String, sSQL As String) As Variant
' Return the value of the field with the name <sField> in the select query <sSQL>.
' The purpose of this is to allow inserting a variable value into the SQL of a query.
Dim oRecSet As DAO.Recordset, nCountRecords As Long, vRetVal As Variant
Set oRecSet = CurrentDb().OpenRecordset(sSQL)
nCountRecords = oRecSet.RecordCount
If (nCountRecords = 0) Then
vRetVal = Null
Else
vRetVal = oRecSet.Fields(sField).Value
End If
vFtnInQry = vRetVal
End Function
So the field definitions changed to:
vFtnInQry("BuyRecDate", "SELECT first([Date]) as BuyRecDate FROM [C:<path>\sources.accdb].[items - subjects] WHERE [Subject name] = """ & [SubjName] & """ and Assessment like ""*Buy*""")) AS FirstBuyRecDate
and
vFtnInQry("SellRecDate", "SELECT first([Date]) as SellRecDate FROM [C:<path>\sources.accdb].[items - subjects] WHERE [Subject name] = """ & [SubjName] & """ and Assessment like ""*Sell*""")) AS FirstSellRecDate
I don't know why this solved the problem. I'm posting it here in case someone else has a mysterious occurrence of "Too many fields defined" from a query that might be solved this way. On the other hand, if someone reading this sees a reason that those subqueries were causing that error, please post about that in a comment or another answer.

Access: Data Type Mismatch using boolean function in query criteria

I have a VBA function IsValidEmail() that returns a boolean. I have a query that calls this function: Expr1: IsValidEmail([E-Mail]). When I run the query, it shows -1 for True and 0 for False. So far so good.
Now I want to filter the query to only show invalid emails. I'm using the Query Designer, so I just add a value of 0 to the Criteria field. This gives me a "Data Type Mismatch" error. So does "0" (with quotes) and False. How am I supposed to specify criteria for a boolean function?
For a boolean column, "0" will definitely give you the "Data type mismatch in criteria expression" error. However, 0 or False without quotes should work. I don't understand why they are generating the same error.
See if you can produce a working query by editing the SQL directly. Create a new query, switch to SQL View and paste in this statement (replacing YourTableName with the name of your table).
SELECT IsValidEmail([E-Mail]) AS valid_email
FROM YourTableName
WHERE IsValidEmail([E-Mail]) = False;
Will your query run without error when you create it that way?
Update: Since that query also produced the same error, all I can suggest is trying this one without any criteria.
SELECT
IsValidEmail([E-Mail]) AS valid_email,
TypeName(IsValidEmail([E-Mail])) AS type_of_valid_email
FROM YourTableName;
However, that seems like a long shot because you already told us your earlier attempt without criteria ran without error. If this doesn't identify the problem, would you consider emailing me a stripped down copy of your database? Let me know if you're interested and I'll give you my email address.
The error was caused by the fact that some of the records in my table have a null E-Mail. My query has a where condition to exclude null E-Mail records, so when I ran it with no condition on the IsValidEmail column my function was only called for records with a non-null E-Mail. However, when I added the condition on IsValidEmail it called the function for every record, and the error came from trying to pass null to a function expecting a string.
Another way to say all that:
SELECT [E-Mail],
IsValidEmail([E-Mail]) <--Executed only for rows matching where clause
FROM Contacts
WHERE IsValidEmail([E-Mail]) = False; <-- Gets executed for all rows
Changing my query expression from IsValidEmail([E-Mail]) to IsValidEmail(nz([E-Mail],"X")) resolved the issue.