How can i know data type of value in textbox?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,
I would like to check data type that store in textbox of my form.
How can i know its data type? By using which function?

Thanks
Gogzilla
 
I assume it is an unbound field? (If it is bound, you can use DAO to
get the data type of the field to which it is bound, and none of the
following comments apply.)

Check out the functions IsNumeric(), IsDate(), and so on.

But - understand that you can not reliably predict the data type, based
only on example values.

For example, say the first value is "True", and the next is "False". Is
that a boolean value? Maybe - if all the /other/ values are also "True"
or "False". But what if one of them (that you haven't seen yet), is
"Maybe"? Or "Fred"? Now it can't be a boolean field, it would have to
be Text. You can not know this just by looking at the first few values.

Take another value, 38814. Is that Numeric? Maybe - maybe not! It could
be a Date instead. (Try debug.print cdate(38814). )

HTH,
TC (MVP Access)
http://tc2.atspace.com
 
Hi Tim

Thank you for your advice. My problem is the unbound field and i would like
to know its data type.
i do not actually know its data type such as Numeric or date type,so i can
not check its type by IsNumeric or IsDate.
Now, i try to use vartype() function to find the answer. It seems OK but i
still would like to know the solution for bound category same as your mention
"If it is bound, you can use DAO to get the data type of the field to which
it is bound"
Could you tell me more detail for the way to solve the bound field problem?

Thanks again

Gogzilla
 
Sorry, you've lost me.

You say: "My problem is the *unbound* field and i would like to know
its data type."

Then you say: "Could you tell me more detail for the way to solve the
*bound* field problem?"

Is it bound - or unbound?

If it's *unbound* - I have already told you the answer.

HTH,
TC (MVP Access)
http://tc2.atspace.com
 
Hi TC

For the unbound problem, i already find the solution but still want to know
the bound case.Could you suggest me for the bound field category?

Thank again,
Gogzilla
 
This will find the data type of the actual field to which a control is
finally bound. Say for example that the form is bound to the following
query:

SELECT MYFIELD AS F1 FROM MYTABLE

If your form was based on that query, and a control was bound to field
F1 in that query, the following code would find the actual field
(MYFIELD), in the actual table (MYTABLE), and return the data type of
that.

The data types are returned as dbXXX constants; eg. 10 = dbText = a
text field.

Dim sTable As String, sField As String
Dim db As Database, fld As Field, o As Object

sTable = Me.RecordSource
sField = Me![txtName].ControlSource ' << uses control txtName for
example.

Set db = CurrentDb()
On Error Resume Next
Set o = db.TableDefs(sTable)
If Err.Number = 0 Then
' record source is a table.
On Error GoTo 0
Set fld = o.Fields(sField)
Else
' not a table - must be a query.
On Error GoTo 0
Set o = db.QueryDefs(sTable)
With o.Fields(sField)
sTable = .SourceTable
sField = .SourceField
End With
Set fld = db.TableDefs(sTable).Fields(sField)
End If
MsgBox "table [" & sTable & "] Field [" & sField & "] data type = " &
fld.Type
Set db = Nothing

HTH,
TC (MVP Access)
http://tc2.atspace.com
 
Hi TC

Thank you for your reply again. i already follow your advice and it can
solve my problem now.

Thanks
Gogzilla
 
Back
Top