Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


If with Variant-type

Posted on 1999-06-24
Medium Priority
Last Modified: 2010-05-02
I have a Variant type which can become to an Integer, a String a Null in my Sub.

I use "If 'variable' Then" and go over the "else" statement to run the source code in case of my 'variable' being "Null".

This works good when 'variable' is an integer - the "if"-clause is "true" - but when 'variable' is a string the error #13 arises (type problem...).

Thanks for your support!
Question by:Stapman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 1519455
Edited text of question.
LVL 22

Expert Comment

ID: 1519456

I'm not sure that I have understood you correctly. Here is an attempt, though. There is an IsNull function that may help. You can also try the IsEmpty function that returns True if the variant variable has not been assigned a value.

If IsNull(x) Then ...

Ture Magnusson
Karlstad, Sweden
LVL 10

Accepted Solution

caraf_g earned 200 total points
ID: 1519457
Without looking at your code it is impossible to tell you what's wrong. But here are some tips.

1 Use IsNull
Dim fred As Variant

fred = Null

If IsNull(fred) Then
    MsgBox "it's null"
End If

2 Use vartype
VarType will return a number depending on the type of variable (See help)
vbEmpty      0      Empty (uninitialized)
vbNull      1      Null (no valid data)
vbInteger      2      Integer
vbLong      3      Long integer
vbSingle      4      Single-precision floating-point number
vbDouble      5      Double-precision floating-point number
vbCurrency      6      Currency value
vbDate      7      Date value
vbString      8      String
vbObject      9      Object
vbError      10      Error value
vbBoolean      11      Boolean value
vbVariant      12      Variant (used only with arrays of variants)
vbDataObject      13      A data access object
vbDecimal      14      Decimal value
vbByte      17      Byte value
vbArray      8192      Array

Dim fred As Variant
fred = CDate("1999/11/11")
If VarType(fred) = vbDate Then 'vbDate = 7
    'It's a Date!!
End If

Need any more information? Then don't accept the question until I've answered all your questions.



Expert Comment

ID: 1519458
Dim Condition As Variant

Condition = 1
If Condition Then MsgBox "True"

Condition = "1"
If CInt(Condition) Then MsgBox "True"

Condition = Null
If IsNull(Condition) Then MsgBox "True"

Author Comment

ID: 1519459
Thanks for your quick and detailed answer(s). Thanks to SLE too!

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question