dcsimg
Results 1 to 9 of 9

Thread: VariantChangeTypeEx Questions

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,765

    VariantChangeTypeEx Questions

    VariantChangeTypeEx() has become one of my favorite toys for coping with cross-locale data interchange in networked applications. But I have a couple of questions about it.

    Does anyone know what this flag really does?

    VARIANT_NOVALUEPROP = 0x01. Prevents the function from attempting to coerce an object to a fundamental type by getting the Value property. Applications should set this flag only if necessary, because it makes their behavior inconsistent with other applications.

    This is a second question, but I was sort of hoping (dreaming?) that the flag above might help get me there:

    Is there a way to use VariantChangeTypeEx() or some related function to accept a String of text and an LCID and "cast the bones" to report back the "likely data type" of the contents of the String?

    For example "3" might report back Byte, Integer, Long, or some generic "numeric" type indication. Or if the LCID is an English language locale a "True" might report back Boolean. A date-time value would report back Date.

    Is there something like this in OLE Automation?

    In other words I'm trying to determine whether there is some easy way to coerce non-String values in a String value to a Variant of the "likely" subtype.


    Or if this is just silly, so be it. But something like this seems to be used by the Jet Text IISAM and ODBC Desktop Text Driver when there is no schema.ini entry for the text file being processed by them. The part of the processing that uses the MaxScanRows setting to guess the data types of columns.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,002

    Re: VariantChangeTypeEx Questions

    Sounds like it applies only to objects? So, if you want to convert an Object to some other variant, sounds like function will query the object's Value property, i.e.: varHandle = Me.Picture. But if you don't want that, would varObjPtr = Me.Picture be a valid conversion? Maybe just playing with it might answer the question ... see what gets returned with/without that flag?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,002

    Re: VariantChangeTypeEx Questions

    Ummm, after re-reading my reply, one might ask if I'm on some medications

    What I meant was that if you tried to convert a variant containing an object to VT_4 (or whatever long is) while using that flag and not using that flag, would one attempt return the picture handle (default value) while another returns the object pointer or something else?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,765

    Re: VariantChangeTypeEx Questions

    You are right, that's what it does.

    Basically it seems to avoid invoking the Value property of the Object, or maybe any defauly property the Object has. More testing required but I think you have put your finger on it.

    I doubt you ever get an Object pointer though, more likely a failure result.

    Thanks.

    I'm not sure my second, and main, question has such an easy answer though.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,002

    Re: VariantChangeTypeEx Questions

    Quote Originally Posted by dilettante View Post
    I'm not sure my second, and main, question has such an easy answer though.
    No, my guesses are likely just as good/bad as yours. We know how many rows Excel scans (with/without registry setting adjustments). We also know how to override that with schema.ini files, all only due to documentation. I haven't seen any documentation on how Excel decides on field vartype without schema files. I think it can be 'reversed engineered' a bit. Likely, more complex vartypes are tested first? If Excel suggests 7FEB is a date, then date takes precedence over text. If 10.194.10.25 is truncated, then numerics take precedence over text, etc, etc. Regarding locales -- that's a good question since it opens up a large range of exceptions: is 7FEB still assumed date if on a locale where FEB is not a month abbreviation?

    Edited: I know your question isn't specifically about Excel. But if one were to accept Excel's behavior as good enough, then one would also have to ask such questions if they chose to try to replicate that behavior in their own routines.

    I recently found this very annoying with a value of 7FEB, from a csv, being converted to date when it was meant to be a text value. What was even more annoying was that while in Excel 2013, if I tried to format the value with a leading tick, formatting the entire column as text, Excel still refused to accept the value as text. Forced me to whip up an ini to get the value imported correctly. Something odd was happening there and didn't want to waste 20+ minutes to figure it out for a one-time csv import into Excel
    Last edited by LaVolpe; Mar 16th, 2019 at 04:36 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,765

    Re: VariantChangeTypeEx Questions

    Would IMEX=1 or another setting force text importing?

    I assume what happens is that the row scanning does something like try several coercions from most to least restrictive, and the first one that succeeds gets a "vote." Then after the number of rows have been scanned the "votes" are tallied and a choice made.

    This probably was never needed generally enough to be worth making it a separate function call, and even if you did you'd have to decide how to pass in a list of values to test and it gets messy pretty quickly. So maybe there is no call to do it in one fell swoop.

    If you need this you can probably implement it case by case as required. Maybe test for Date first, then Single, then Double, then Long, then Integer, then Byte, then Boolean. That leaves out Currency and Decimal of course but if you wanted those you could add them to the list of "probings" somewhere.

    The voting thing can be a problem. Imagine a column of what you wanted to be Longs but most of the values fit in a Byte. By the "voting" Byte would get picked, then the few values too large by rights should return as Null - but again if you are writing your own anyway you could choose some other "default" like Empty or even 0 if that made sense.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,765

    Re: VariantChangeTypeEx Questions

    I did a small test and a "7FEB" failed when I tried coercing to Date with a Finnish locale.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,179

    Re: VariantChangeTypeEx Questions

    You know? I've been thinking about this, and I think it can be made into a pure VB6 question. Doesn't this video illustrate the question? The top-right window is just my test file pulled up in NP++.



    There's not much to it, but here's the code. You'll have to patch up the path to the test file, and make your own test file.

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim v As Variant
        Const sFile = "C:\Users\Elroy\Desktop\New folder\ttt.txt"
    
        Open sFile For Input As 1
        Input #1, v
        Close 1
    
        Debug.Print TypeName(v)
    
        Unload Me
    End Sub
    
    

    I agree that it'd be nice if we knew the precise algorithm that was used to make these Type determinations.

    Hmmm, I tested 7FEB and it came out as Integer in the above VB6 test. However, it clearly comes out as Date if pasted into Excel. So maybe it's not precisely the same algorithm.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,765

    Re: VariantChangeTypeEx Questions

    I haven't been able to figure out how much of VB6's native text I/O makes use of Automation functions and how much uses its own logic within the runtime.

    I suspect the latter because (a.) code for this existed in versions of MS Basic long before Automation or even Windows, and (b.) as you noted there are still differences in outcome here and there.

    VB was an evolving product... up until it was killed off.

    Perhaps the parts of VB6 that are locale-aware are those that had been transitioned to use Automation and the locale-blind operations (examples: native text I/O, the Str$() and Val() functions) are legacy code not yet removed in order to assist in backward compatibility.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width