Results 1 to 12 of 12

Thread: CrashMaker

  1. #1
    Join Date
    Mar 2009
    Location
    SLOVAKIA,Bratislava
    Posts
    70

    Lightbulb Unanswered: CrashMaker

    this SOMETIMES crashes, but in one build allways...
    dim lcItem$
    lcItem = Replace(lcItem, Chr(0), "")
    -----
    lcItem = Replace(lcItem, Chr$(0), "")...and this NOT
    15 years db-programmer(dBase,FoxPro,MS Access 2002/2003),Symbian C++

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You should not mix both types of variables declaration it's looking for trouble, and the suffix method ($, %, # etc.) is obsolete anyway).

    So use (better not!):
    Code:
    Dim lcItem$
    lcItem = Replace(lcItem, Chr$(0), "")
    Or:
    Code:
    Dim lcItem As String
    lcItem = Replace(lcItem, Chr(0), "") ' vbNullChar also works in place of Chr(0)
    Have a nice day!

  3. #3
    Join Date
    Mar 2009
    Location
    SLOVAKIA,Bratislava
    Posts
    70
    Hi, thank, O.K. maybe ...
    Code:
    Dim lcItem As String ' NOT lcItem$
    lcItem = Replace(lcItem, vbNullChar, vbNullString) 'OK
    -------------------
    You should not mix both types of variables declaration it's looking for trouble, and the suffix method ($, %, # etc.) is obsolete anyway)....
    -----
    This may be a problem, i am using it EVERYWHERE
    Is this somewhere
    in Microsoft as ***Un-recommended usage*** declared ?
    Last edited by pacala_ba; 04-04-09 at 07:29.
    15 years db-programmer(dBase,FoxPro,MS Access 2002/2003),Symbian C++

  4. #4
    Join Date
    Mar 2009
    Location
    SLOVAKIA,Bratislava
    Posts
    70
    from another forum:
    Your Dim statement will create a Variant variable and not a String, in case you didn't know that. The $ means very little in VBA.
    Is that TRUE ?
    15 years db-programmer(dBase,FoxPro,MS Access 2002/2003),Symbian C++

  5. #5
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Since you're trying to replace a Null, Chr(0), with a Zero-length string, why not simply use

    lcItem = Nz(lcItem, "")
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    Join Date
    Mar 2009
    Location
    SLOVAKIA,Bratislava
    Posts
    70
    Null , vbNullChar...is NOT the same !!!

    All this variants seems, to works...
    Code:
    Sub test_1()
    Dim lc_String0 As Variant, lc_String1$, lc_String2 As String
    Dim lb_type As Boolean
    lc_String1 = "AAA" & vbNullChar & vbNullChar
    lc_String2 = "AAA" & vbNullChar & vbNullChar
    lc_String0 = "AAA" & vbNullChar & vbNullChar
    lc_String0 = Replace(lc_String0, Chr(0), "")
    lc_String1 = Replace(lc_String1, Chr(0), "")
    lc_String2 = Replace(lc_String2, vbNullChar, vbNullString)
    lb_type = (VarType(lc_String0) = vbString)
    lb_type = (VarType(lc_String1) = vbString)
    lb_type = (VarType(lc_String2) = vbString)
    End Sub
    15 years db-programmer(dBase,FoxPro,MS Access 2002/2003),Symbian C++

  7. #7
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    From MSDN July 1999 (not a novelty!):
    Type Declaration Characters

    Why should we have to remember that % is an integer or # a double? There are no type declaration characters for newer data types such as Boolean or date. Those who designed Visual Basic must have run out of punctuation or decided it was a bad idea. Type declaration characters are hangovers from pre–Visual Basic forms of Basic, but now we are just left with an inconsistent mess.
    About string, see also:
    Chapter 6 Strings

    Have a nice day!

  8. #8
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    There seems to be a confusion between a string variable and a variant variable of type string.

    If you want to be sure wether a variable is a "true" string variable or a variant of string type, try to assign Null to it: with a variant it's possible, with a "true" string you get an error.

    Have a nice day!

  9. #9
    Join Date
    Mar 2009
    Location
    SLOVAKIA,Bratislava
    Posts
    70
    Hi, O.K.
    i am now replacing ~36,000 Dollar signs.....
    15 years db-programmer(dBase,FoxPro,MS Access 2002/2003),Symbian C++

  10. #10
    Join Date
    Mar 2009
    Location
    SLOVAKIA,Bratislava
    Posts
    70
    Code:
    Sub test_1()
    On Error GoTo Error_handler
    Dim lc_String0 As Variant, lc_String1$, lc_String2 As String
    Dim lb_type As Boolean
    lc_String1 = "AAA" & vbNullChar & vbNullChar
    lc_String2 = "AAA" & vbNullChar & vbNullChar
    lc_String0 = "AAA" & vbNullChar & vbNullChar
    lc_String0 = Replace(lc_String0, Chr(0), "")
    lc_String1 = Replace(lc_String1, Chr(0), "")
    lc_String2 = Replace(lc_String2, vbNullChar, vbNullString)
    lb_type = (VarType(lc_String0) = vbString)
    lb_type = (VarType(lc_String1) = vbString)
    lb_type = (VarType(lc_String2) = vbString)
    lc_String0 = Null ' o.k.
    lc_String1 = Null ' 94           Invalid use of Null
    lc_String2 = Null ' 94           Invalid use of Null
    Exit Sub
    Error_handler:
        Debug.Print Err, Err.Description
    Resume Next
    End Sub
    15 years db-programmer(dBase,FoxPro,MS Access 2002/2003),Symbian C++

  11. #11
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    From MSDN also:
    VarType Function

    Returns an Integer indicating the subtype of a variable.

    Syntax

    VarType(varname)

    The required varname argument is a Variant containing any variable except a variable of a user-defined type.

    Return Values

    Constant, Value, Description
    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 witharrays of variants)
    vbDataObject, 13, A data access object
    vbDecimal, 14, Decimal value
    vbByte, 17, Byte value
    vbUserDefinedType, 36, Variants that contain user-defined types
    vbArray, 8192, Array
    Many implicit conversions happen in Access (in VB/VBA in general) and most of the time it's all right. However there are some circumstances where an implicit conversion goes wrong (not all right at all!) and this can leads to many problems, from code not producing the expected result to Access crashes.

    The same is true when you blindly rely on the default property of an object: Most of the time it's all right inside Access, but if your code uses some external references such as to the Scripting Runtime (to use a Dictionary object for instance), to Windows Common Controls etc... you can get very strange results if you write something like: MyDictionary.Add Me.TextBox1 when you mean MyDictionary.Add Me.TextBox1.Value. It can quickly become very messy.

    Have a nice day!

  12. #12
    Join Date
    Mar 2009
    Location
    SLOVAKIA,Bratislava
    Posts
    70
    Citat from another place....
    Type declaraition suffixes are definately still valid, as Ace has stated.

    INFO: Type Declaration Character to Data Type Chart

    ----

    There is NO difference in how the memory is allocated with the different methods. Also, despite their validity, the technique seems to be pretty much a depricated method, which basically means, its supported but probably won't be furthered or encouraged (as evidenced by the fact that the newer VBA datatypes do not have a declaration character). To me, it does make your code more difficult to interpret initially for those not familiar with the technique.

    --------------------
    Brent Spaulding | datAdrenaline | Access MVP
    Pay it Forward ... It will enrich the lives of many!
    15 years db-programmer(dBase,FoxPro,MS Access 2002/2003),Symbian C++

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •