473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

determining variable type 'compatibility'

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:
Expand|Select|Wrap|Line Numbers
  1. function CanBeDate(str)
  2. If str 'can be a date' then
  3.   CanBeDate = True
  4. else
  5.   CanBeDate = False
  6. End If
  7. End Function
From here, it would be easy to handle the error (a simple
Expand|Select|Wrap|Line Numbers
  1. 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'"
Mar 23 '11 #1

✓ answered by NeoPa

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
TheSmileyCoder
2,322 Expert Mod 2GB
There is a built in function called isDate and isNumeric.
Example usage:
Expand|Select|Wrap|Line Numbers
  1. ? isdate("12/2/20x[38475934f59dr.gpi9087304d0i") returns False
  2. ? isdate("12/2/20") returns True
Mar 23 '11 #2
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).
Mar 23 '11 #3
ADezii
8,834 Expert 8TB
@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:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As Database
  2. Dim rst As DAO.Recordset
  3. Dim intFldctr As Integer
  4.  
  5. Set MyDB = CurrentDb
  6. Set rst = MyDB.OpenRecordset(conTABLE_NAME, dbOpenSnapshot)
  7.  
  8. Debug.Print "------------------------------------------------------------------"
  9. Debug.Print rst.Fields(0).Name, rst.Fields(2).Name, rst.Fields(3).Name, rst.Fields(5).Name
  10. Debug.Print "------------------------------------------------------------------"
  11.  
  12. With rst
  13.   Do While Not .EOF
  14.     If Not IsDate(.Fields(2)) Or Not IsDate(.Fields(3)) Or Not IsNumeric(.Fields(5)) Then
  15.       Debug.Print rst.Fields(0), IIf(Not IsDate(.Fields(2)), "Invalid Date", "OK"), _
  16.                                  IIf(Not IsDate(.Fields(3)), "Invalid Date", "OK"), _
  17.                                  IIf(Not IsNumeric(.Fields(5)), "Invalid Number", "OK")
  18.     End If
  19.      .MoveNext
  20.   Loop
  21. End With
  22.  
  23. rst.Close
  24. Set rst = Nothing
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. ------------------------------------------------------------------
  2. OrderID       OrderDate     RequiredDate  ShipVia
  3. ------------------------------------------------------------------
  4.  10600        OK            OK            Invalid Number
  5.  10603        OK            OK            Invalid Number
  6.  10611        Invalid Date  OK            Invalid Number
  7.  10616        Invalid Date  Invalid Date  OK
  8.  10620        Invalid Date  OK            OK
  9.  10627        OK            Invalid Date  OK
  10.  10633        Invalid Date  OK            OK
  11.  10640        OK            OK            Invalid Number
  12.  10660        Invalid Date  OK            OK
  13.  10668        OK            Invalid Date  OK
  14.  10669        OK            OK            Invalid Number
  15.  10676        Invalid Date  Invalid Date  OK
  16.  10687        OK            Invalid Date  OK
  17.  10693        Invalid Date  OK            OK
  18.  10697        OK            Invalid Date  OK
  19.  10728        Invalid Date  Invalid Date  Invalid Number
  20.  10799        Invalid Date  OK            OK
Mar 23 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
4
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...
5
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...
2
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?
1
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 ...
9
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...
5
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...
1
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...
4
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...
2
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...
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.