i am importing data from many text files which sometimes contain an error. These text files include dates, strings, and integers, but in order to make this program as hands-free as possible, i need to import all fields as text - in the event of an error in the text field, a field that is usually a date may actually turn out to be something like "12/2/20x[38475934f59dr.gpi9087304d0i". If i try to set the field in the destination table to accept nothing but dates, this will cause a run-time error, which requires human intervention (particularly mine).
Anyway, importing is no longer a problem, as long as i set everythng to text. However, I run into basically the same problem when i attempt to direct the data to its appropriate permanent table, as those must be "data-type" specific. Is there some function that can determine if a value is compatible with a variable type?
i.e. consider the pseudo-function: - function CanBeDate(str)
-
If str 'can be a date' then
-
CanBeDate = True
-
else
-
CanBeDate = False
-
End If
-
End Function
From here, it would be easy to handle the error (a simple - if nz(dlookup(True,"[Table]","CanBeDate([Field])=False"),False) then goto handle_errors
would be sufficient)
It's the second line of code that is my question - is there a function already in VB or even a simple one I could write to test "If str 'can be a date'"
There is a whole set of functions starting Is... which can be used to determine whether strings are compatible with various datatypes. IsDate() is the first one you need of course. If you look it up under Help you'll find links to all the others that are available (under See Also).
3 1948
There is a built in function called isDate and isNumeric.
Example usage: - ? isdate("12/2/20x[38475934f59dr.gpi9087304d0i") returns False
-
? isdate("12/2/20") returns True
NeoPa 32,556
Expert Mod 16PB
There is a whole set of functions starting Is... which can be used to determine whether strings are compatible with various datatypes. IsDate() is the first one you need of course. If you look it up under Help you'll find links to all the others that are available (under See Also).
@normalice - Like yourself, and especially for larger Imports, I will Import all the Data as Text into a Temporary Table. I will then write Custom Code to make sure that all Field Data types match those of the MASTER. The following Code will create a Recordset based on the Imported Data, then check Fields 3 and 4 to make sure that they are in a Valid Date Format. It will also check Field 6 which must be Numeric. If either of these Fields does not have the correct Data Type, then I will post, in some manner, the Primary Key to uniquely identify the Field(s) in question, as well as the offending Fields. Null Values will show up as Invalid Data Type, but that can easily be adjusted. I'll simply post my Code, and should you have any questions, please feel free to ask: - Dim MyDB As Database
-
Dim rst As DAO.Recordset
-
Dim intFldctr As Integer
-
-
Set MyDB = CurrentDb
-
Set rst = MyDB.OpenRecordset(conTABLE_NAME, dbOpenSnapshot)
-
-
Debug.Print "------------------------------------------------------------------"
-
Debug.Print rst.Fields(0).Name, rst.Fields(2).Name, rst.Fields(3).Name, rst.Fields(5).Name
-
Debug.Print "------------------------------------------------------------------"
-
-
With rst
-
Do While Not .EOF
-
If Not IsDate(.Fields(2)) Or Not IsDate(.Fields(3)) Or Not IsNumeric(.Fields(5)) Then
-
Debug.Print rst.Fields(0), IIf(Not IsDate(.Fields(2)), "Invalid Date", "OK"), _
-
IIf(Not IsDate(.Fields(3)), "Invalid Date", "OK"), _
-
IIf(Not IsNumeric(.Fields(5)), "Invalid Number", "OK")
-
End If
-
.MoveNext
-
Loop
-
End With
-
-
rst.Close
-
Set rst = Nothing
Sample OUTPUT: - ------------------------------------------------------------------
-
OrderID OrderDate RequiredDate ShipVia
-
------------------------------------------------------------------
-
10600 OK OK Invalid Number
-
10603 OK OK Invalid Number
-
10611 Invalid Date OK Invalid Number
-
10616 Invalid Date Invalid Date OK
-
10620 Invalid Date OK OK
-
10627 OK Invalid Date OK
-
10633 Invalid Date OK OK
-
10640 OK OK Invalid Number
-
10660 Invalid Date OK OK
-
10668 OK Invalid Date OK
-
10669 OK OK Invalid Number
-
10676 Invalid Date Invalid Date OK
-
10687 OK Invalid Date OK
-
10693 Invalid Date OK OK
-
10697 OK Invalid Date OK
-
10728 Invalid Date Invalid Date Invalid Number
-
10799 Invalid Date OK OK
Sign in to post your reply or Sign up for a free account.
Similar topics
by: bartek d |
last post by:
Hello,
Regarding my previous question about a class which is used to store a
variable type vector. I tried to be more elaborate on the code.
I'd be grateful for your suggestions. Am I going in...
|
by: MCollins |
last post by:
trying to determine a variable type, specifically that a variable is an
integer.
i tried using type(var) but that only seemed to produce a response in the
command line.
is there a built in...
|
by: frankhall36 |
last post by:
Yeah, I need some help, I'm not a very good programmer but I've tried a
lot of languages, and anyways, I want to start applying programming to
physics, and I would like to learn how to make a...
|
by: Mike Moore |
last post by:
does anyone have an example of how to get the connection string object
converted to a string variable type in order for me to call a function?
|
by: tshad |
last post by:
I have some code to go through a session collection for my error page
routine and I get an error on my objects that I store in session variables.
Dim strName as String
Dim iLoop as Integer
...
|
by: Schraalhans Keukenmeester |
last post by:
I have some C functions (with variable length argument lists) that use
void pointers as arguments.
Is there a way to determine at runtime what type of parameter is
actually passed on to the...
|
by: John |
last post by:
Which variable type (c#) can whole the largest whole number? I know this
sounds silly but as double and decimal are made for numbers with decimals I
am not sure.
Also if anybody knows of any...
|
by: Sharon |
last post by:
I need to write an XML document, that other users can work with to change
values and to add elements.
My problem is that for each element that me or any other user will add,
should have some...
|
by: mouseit |
last post by:
If I have a variable of some sort, say x, how can I find out what
variable type javascript thinks it is? For example, if I've declared x
as 5 earlier, how do I know now whether it's a string or a...
|
by: Dan29 |
last post by:
Hi guys,
I wonder if you can help? I am trying to create Query Definitions in VBA in Access 2002.
I'm using the book 'Begining Access 2002 VBA', which shows how to do this. On the example...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |