Consulting

Results 1 to 18 of 18

Thread: Solved: Tell Me About the StrPtr Function

  1. #1

    Solved: Tell Me About the StrPtr Function

    Quote Originally Posted by ALe
    there's the StrPtr function that can help you to determine if user pressed cancel. See below



    VBA:


    Sub CheckIfCancelWasPressed()

    Dim Response As String
    Response = InputBox("Your string here:")
    If StrPtr(Response) = 0 Then MsgBox "Cancel was pressed!"
    End Sub
    This is super cool! Where is the "StrPtr" function described? Help didn't seem to know anything about it, but when I used it, it worked.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    StrPtr (like VarPtr, VarPtrArray, VarPtrStringArray, and ObjPtr) is an undocumented function that is used to get the underlying memory address of variables.

    As it is undocumented, I would suggest not using it, it certainly isn't needed where ALe used it.

    [vba]

    Sub CheckIfCancelWasPressed()
    Dim Response As String
    Response = InputBox("Your string here:")
    If Response = "" Then MsgBox "Cancel was pressed!"
    End Sub
    [/vba]

  3. #3
    Actually the code change you showed is what I have always used, except I use vbNullString instead of "". It looked like maybe StrPtr might have some neat characteristics to explore. Oh, well ...

    Thanks xld!

  4. #4
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by Cyberdude
    Actually the code change you showed is what I have always used, except I use vbNullString instead of "". It looked like maybe StrPtr might have some neat characteristics to explore. Oh, well ...

    Thanks xld!
    If anything's undocumented then at any time in the future it's use may be unsupported.

    Also, check out the Help files: vbNullString - String having value 0 (Not the same as a zero-length string ("")) used for calling external procedures.


    EDIT: Also, if it's really an issue as to whether the user presses OK or cancel for an empty string entry, you should be using the InputBox method and not the InputBox function because the method is specifically designed to provide selective validation of user input where-as the function is not. e.g. [vba]Sub CheckIfCancelWasPressed2()
    Dim Response As String
    Response = Application.InputBox("Your string here:", , , , , , , 1 + 2)
    If Response = "False" Then MsgBox "Cancel was pressed!"
    End Sub[/vba](you'll notice here that Visual Basic is able to discern the "False" generated by clicking the Cancel button as being different to a "False" being typed in the input box as an entry )
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by johnske
    If anything's undocumented then at any time in the future it's use may be unsupported.
    It is already gone in VB 2005.

  6. #6
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by johnske
    EDIT: Also, if it's really an issue as to whether the user presses OK or cancel for an empty string entry, you should be using the InputBox method and not the InputBox function because the method is specifically designed to provide selective validation of user input where-as the function is not. e.g. [vba]Sub CheckIfCancelWasPressed2()
    Dim Response As String
    Response = Application.InputBox("Your string here:", , , , , , , 1 + 2)
    If Response = "False" Then MsgBox "Cancel was pressed!"
    End Sub[/vba](you'll notice here that Visual Basic is able to discern the "False" generated by clicking the Cancel button as being different to a "False" being typed in the input box as an entry )
    I don't understand where there's a point to having both a function AND a method for InputBox.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  7. #7
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by malik641
    I don't understand where there's a point to having both a function AND a method for InputBox.
    The InputBox "function" is a broader 'general purpose' or 'cross-application' VBA function that's part of the VBA model. It's also available for use in (for example) Word as well as Excel.

    On the other hand, the InputBox "method" (Application.InputBox, or more specifically, Excel.Application.InputBox) is quite specific to the Excel object model and can't be used in Word.

    The generic "function" is only designed to handle strings, where-as the more specific "method" is designed to allow for data validation of user input in Excel.


    EDIT: Because the 'method' is designed specifically for Excel then, by rights, we should only ever use the InputBox method for Excel. But often the 'function' will suffice for the job at hand (and we all get lazy from time to time don't we? )
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Thanks for clearing that up John

    So I guess the InputBox function is native to VB?

    Quote Originally Posted by johnske
    (and we all get lazy from time to time don't we? )
    Of COURSE!!!!!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  9. #9
    Regarding the use of vbNullString instead of "", at one time there was a lot of discussion on this topic. The conclusion was that it is more efficient to use vbNullString because (as I recall) the "" creates a seperate constant for each occurrence, whereas vbNullString doesn't create a constant.

    I am quite surprised at your dissenting comments, John, because I thought it had been made very clear that vbNullString is preferable. Hmmmm.

  10. #10
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    vbNullString - String having value 0 (Not the same as a zero-length string ("")) used for calling external procedures
    Didn't mean to start any arguments, but this was a quote straight out of the VBA help files
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  11. #11
    It's not a matter of arguing ... I'm just trying to get to the truth of the matter. I remember being referred to another link that went into detail defending vbNullString. The claim was (as I remember) that this was a Microsoft recommendation. Sorry, but I can't remember the details. Well, it's not a biggy, although I've been using it with no problems. Hmmmm.

  12. #12
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    StrPtr (like VarPtr, VarPtrArray, VarPtrStringArray, and ObjPtr) is an undocumented function that is used to get the underlying memory address of variables.

    As it is undocumented, I would suggest not using it, it certainly isn't needed where ALe used it.


    VBA:
    Sub CheckIfCancelWasPressed() Dim Response As String Response = InputBox("Your string here:") If Response = "" Then MsgBox "Cancel was pressed!" End Sub
    The advantage of StrPtr is that it tells you "Cancel" was pressed. With other solutions, if the inputbox is left blank, you can't so easily understand wheter "OK" or "Cancel" was pressed.
    ALe
    Help indigent families: www.bancomadreteresa.org

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    But the HUGE disadvantage is that because it is unsupported, you cannot guarantee it is in the next release. One day your perfectly working code stops working.

  14. #14
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Yes it is true, it is a temporary solution and shouldn't be used for "long run projects".

    BTW as Keynes said "In the long run we are all dead"

    ALe
    Help indigent families: www.bancomadreteresa.org

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by ALe
    Yes it is true, it is a temporary solution and shouldn't be used for "long run projects".

    BTW as Keynes said "In the long run we are all dead"

    But in the medium run, we want life to be as pleasant as possible.

  16. #16

    Thank you "StrPtr"

    Quote Originally Posted by xld View Post
    But in the medium run, we want life to be as pleasant as possible.
    Folks, I know this is a very very old old thread (at least in coding timescale) but having just discovered it and read through it all, I thought it's worth adding my bit. Everybody has argued the issue from a particular perspective that suits them but I don't think anyone has from my perspective.

    Yes Application.InputBox is all singing and all dancing and does everything that you would like to do with InputBox. I quite like its compactness too. However, it has annoyed me too many times. Have you ever tried to edit any text in it by using any of the navigation keys - all 10 of them? It immediately starts populating the box with cell addresses and formulae and basically ruins the text you're trying to edit. I have not found any way of turning this behaviour off. If anyone does, please tell me.

    Plain InputBox on the other hand doesn't know the difference between a Cancel button and a null string which is a pain when you need to know whether the user has truly cancelled or just got trigger happy and returned a blank. Here, StrPtr comes to the rescue and I like it. So I'm using it.

    Like ALe said: "In the long run, we're all dead. In the meantime, we should make life as pleasant as possible." I will use it and trap any possible future errors it might throw up, enjoy my code and leave the rest to fate (aka MicroSoft). Hey! It still works in Excel 2010. Not tried it yet in 2016 but will shortly. If it's aint dead yet, may be it won't.

  17. #17
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Have you tried F2 prior to editing?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  18. #18
    I was just searching for a way to handle cancels with an InputBox and found this thread. I'm using VBA with Access 2010 and the strptr() function still works in this version. I assume this function is part of the VB for Applications reference..? Anyway, I thought it was funny that it still works LOL.

Posting Permissions

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