How do I declare vbYesNo?

I use this in my module whenever I want to display a message or prompt:

Public Sub msgDisp(strMsg As String, 'vbInformation As ???)
 Dim strResp As String
    strResp = MsgBox(strMsg, intBtn, App.ProductName)
    If strResp = 0 Then Stop
End Sub

I'm trying to clean up my ollllldddd code - and want to declare all my variables - so how do I declare the latter portion which identifies if it's going to be Ok, yes/no, Info/Question, etc...
LVL 67
sirbountyAsked:
Who is Participating?
 
fds_fatboyConnect With a Mentor Commented:
SirBounty
Never use plus sign, use Or.

Call msgDisp(strMsg, vbYesNo OR vbQuestion)

Public Sub msgDisp(ByVal strMsg As String, ByVal vbInformation As VBA.VbMsgBoxStyle)
 Dim lngResp As VBA.VbMsgBoxResult
    lngResp = MsgBox(strMsg, intBtn, App.ProductName)
    If lngResp = 0 Then Stop
End Sub
0
 
fds_fatboyCommented:
For a start strResp should not be a string - it is a numerator based on a Long int.

Public Sub msgDisp(strMsg As String, vbInformation As VBA.VbMsgBoxStyle)
 Dim lngResp As VBA.VbMsgBoxResult
    lngResp = MsgBox(strMsg, intBtn, App.ProductName)
    If lngResp = 0 Then Stop
End Sub
0
 
learning_t0_pr0gramCommented:
Dim blah As VbMsgBoxResult
Dim blah2 As VbMsgBoxStyle
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
merowingerCommented:
hi,


-vbAbortRetryIgnore
-vbApplicationModal
-vbCritical
-vbDefaultButton1
-vbDefaultButton2
-vbDefaultButton3
-vbDefaultButton4
-vbExclamation
-vbInformation
-vbMsgBoxHelpButton
-vbMSgBoxRight
-vbMsgBoxRtlReading
-vbMsgBoxSetForeground
-vbOKCancel
-vbOKOnly
-vbQuestion
-vbRetryCancel
-vbSystemModal
-vbYesNo
-vbYesNoCancel
0
 
fds_fatboyCommented:
I should have pointed out that the above requires you to have a reference to Visual Basic For Applications (MSVBVM60.DLL) - but I would be very surprised if you didn't already have this.
0
 
sirbountyAuthor Commented:
fds_fatboy - okay, but now I get ByRef arg. mismatch on the following :

Call msgDisp(strMsg, vbYesNo + vbQuestion)

Using:
Public Sub msgDisp(strMsg As String, vbInfo As VBA.VbMsgBoxStyle)
 Dim lngResp As VBA.VbMsgBoxResult
    lngResp = MsgBox(strMsg, vbInfo, App.ProductName)
    If lngResp = 0 Then Stop
End Sub
0
 
sirbountyAuthor Commented:
Aha - I probably don't...brb
0
 
sirbountyAuthor Commented:
Well, on 2nd glance - why would I need that?  Isn't there a way to ref it without using VBA?
0
 
David LeeCommented:
You don't need to declare them, they're constants.  Most, if not all of them, are integers.  So, if you're going to declare a variable to receive them and want to make sure it's of the right type, then I'd suggest using Variant.
0
 
sirbountyAuthor Commented:
Hey BDF - 'not' declaring them is actually declaring them as a variant, right?  So, there's no way to declare it as 'something'? (that takes up less mem than Var)
0
 
fds_fatboyCommented:
>>Hey BDF - 'not' declaring them is actually declaring them as a variant, right?
This is only the case if you are running without Option Explicit set. This is not recommended.
They are constants set up inside VBA.

Do you not have Object Browser?
0
 
Erick37Commented:
I would use a Long data type, not the VBMsgBoxResult.  Think ahead to porting to .NET or some other language.  It may be easier in the long run to keep it simple.

Call msgDisp(strMsg, vbYesNo OR vbQuestion)

Public Sub msgDisp(ByVal strMsg As String, ByVal vbInformation As Long)
Dim lngResp As Long
    lngResp = MsgBox(strMsg, intBtn, App.ProductName)
    If lngResp = 0 Then
        'Not sure what Stop does
        'Unload Me '<< Unload the form?
    End If
End Sub
0
 
fds_fatboyCommented:
>> Well, on 2nd glance - why would I need that?  Isn't there a way to ref it without using VBA?

VBA is an integral part of VB. The basic string handling, data conversion, environment, OS calling and math fuctions all live in there amongst others. Remove the reference and you will cripple VB.
0
 
sirbountyAuthor Commented:
yea, I don't have Option Explicit - trying to clean up before I place that in there.
I don't think I want to add the VBA reference if I don't have to...isn't there a way to accomplish this without it?
0
 
fds_fatboyCommented:
Erick37
>> I would use a Long data type, not the VBMsgBoxResult.  Think ahead to porting to .NET or some other language.  It may be easier in the long run to keep it simple.

Why? All the standard VB constants and their datatypes have equivalents in dotNet. In fact it is Microsoft recommend the correct constants and their datatypes are used to aid the porting process:
0
 
sirbountyAuthor Commented:
Actually, I do already have that reference fds_fatboy...Hmm-  Struggling a bit with this one now - the code doesn't seem to be doing what I remember having it do way back when.

Basically, I want to have a sub or function in my module msgDisp() that will either just display a message with OKonly or accept a yes/no response.  

I hope to eventually move to .Net, but can't see that happening for at least a year...
0
 
sirbountyAuthor Commented:
Okay, using your code fds_fatboy, it 'works' but doesn't produce the yes/no - it's only an Ok from:

Call msgDisp(strMsg, (vbYesNo And vbQuestion))

I don't want an "or" cause I want the Yes/No buttons WITH the Question image...
0
 
sirbountyAuthor Commented:
Bumping the points - thought this would be a simple one... :D
0
 
sirbountyAuthor Commented:
Ok, if I use Call msgDisp(strMsg, 36) it works with yes/no - but I would rather use the vb'names' equivalent - hopefully without having to add it to another variable...any clue?
0
 
fds_fatboyCommented:
sb:
In my experience it would be easier to put the option explicit in first and when the compiler will aid you finding undeclared variables.

>> I don't think I want to add the VBA reference if I don't have to...isn't there a way to accomplish this without it?

What kind of project have you got? I don't believe any will allow you not to have a reference to VBA (but I may be wrong, I've never wanted to try it)

The only alternative would be to create your own constants or enumerations. The easiest method would be to use the object browser in VBA and copy them out, or look them up in MSDN and copy them from there.
0
 
jimbobmcgeeCommented:
When you are typing the code for MsgBox, if it comes up with the syntax in a tooltip, then you have the reference and can use the constants, vbYes, vbOK, vbInformation, etc.  Otherwise, you could use the sum of the following to get the desired results:

Message box types:
VBOKOnly = 0
VBOKCancel = 1
VBAbortRetryIgnore = 2
VBYesNoCancel = 3
VBYesNo = 4
VBRetryCancel = 5
VBCritical = 16
VBQuestion = 32
VBExclaimation = 48
VBInformation = 64
VBDefaultButton1 = 0
VBDefaultButton2 = 256
VBDefaultButton3 = 512
VBDefaultButton4 = 768
VBApplicationModal = 0
VBSystemModal = 4098

Message box return values
VBOK = 1
VBCancel = 2
VBAbort = 3
VBRetry = 4
VBIgnore = 5
VBYes = 6
VBNo = 7

So you could say

   If MsgBox("Choose Yes or No", 36, "Choose...") = 6 then
      ' ----- Do yes code
   Else
      ' ----- Do no code
   End If

HTH
0
 
fds_fatboyCommented:
sb:

>> I don't want an "or" cause I want the Yes/No buttons WITH the Question

USE MY SOLUTION - And will not work - this is binary logic.

I suggest you LEARN BASIC LOGIC GATES, it will stop us both wasting our time - use the OR. You will  get the "Yes/No buttons WITH the Question"

AND gate
I1     I2     R
0      0      0
0      1      0
1      0      0
1      1      1

OR gate
I1     I2     R
0      0      0
0      1      1
1      0      1
1      1      1
0
 
Erick37Connect With a Mentor Commented:
The base type of the vbMsgBoxResult is Long as shown here,

Dim x As VbMsgBoxResult
Debug.Print VarType(x) = vbLong

The only reason to use it in declaring a variable would be for the help which VB gives you when coding.

e.g.

Dim x as long

when you type   If x = ...  There is no dropdown tip

On the other hand

Dim x As vbMsgBoxResult

if x = ...  VB will list all of the constants for you as you type.

Porting may not be an issue, as fatboy pointed out.  Personally I would use a Long data type and a descriptive variable name.

Dim lmsgRetrurn As Long
Dim lmsgStyle As Long

lmsgStyle = vbInformation Or vbOKOnly

lmsgReturn = MsgBox(...
0
 
sirbountyAuthor Commented:
Ok, it works, but (and I apologize for wasting your time)
How does

vbyesNo(4) AND vbQuestion (32) not work,
36 'does' work
but
vbyesno OR vbquestion works?

The logic that I read this as doesn't tell me how you would receive the same value.

I understand it the way that you've posted Erick37, but not completely the way fds_fatboy suggests.
Just trying to learn 'how' to ride the bike before I take off the training wheels... :)

0
 
jimbobmcgeeCommented:
fds_fatboy:

Out of interest, why do you prefer to use OR instead of + in your MsgBox calls; surely when dealing with numbers you would prefer to add them.  Is there some caveat I'm not familiar with?
0
 
Erick37Commented:
To combine styles, use the Or operator.  This performs a bitwise Or operation, not a Logical operation.

e.g.

lmsgResult = MsgBox("Info", vbInformation Or vbOkOnly, App.Title)
0
 
sirbountyAuthor Commented:
Glad to know I'm not the only one struggling with this...
I had this project working (though admittedly it was crap coding) a few years back using + and it worked as expected...
I've since taken a couple of courses in VB and working on a third and trying to 'better' my old apps by cleaning them up 'the proper' way... : )
0
 
jimbobmcgeeConnect With a Mentor Commented:
>> vbyesNo(4) AND vbQuestion (32) not work

I think because AND is a bitwise logical and (which is a bitwise multiplication).

Take, for example, IP addresses and subnet masks:

    ip:  192.168.64.1     = binary 11000000.10011000.01000000.00000001
    sn: 255.255.255.0   = binary 11111111.11111111.11111111.00000000

since 1 x 1 = 1, 1 x 0 = 0, 0 x 0 = 0 and 0 x 0 = 0

    AND: 192.168.64.0  = binary 11000000.10011000.01000000.00000001

HTH (a little bit!!)

0
 
jimbobmcgeeConnect With a Mentor Commented:
Erick37:
 >> This performs a bitwise Or operation, not a Logical operation.

Is this because MsgBox converts the integer into bits and uses each bit as a boolean flag for the msgbox property, e.g.:

                       vbQuestion = 32 = binary 00100000
                       vbYesNo     = 4  = binary 00000100
   
          vbQuestion AND vbYesNo = binary 00000000 = 0  (each bit is multiplied)
            vbQuestion OR vbYesNo = binary 00100100 = 36  (each bit is added)

(Don't answer that, Erick -- I've just answered my own question!!)
0
 
sirbountyAuthor Commented:
No, that helps a LOT.  Networking/ANDing I understand.  Haha.  So VB's "OR" (bitwise compares) are like ANDing IP addresses...
Cool - thanx.
I'll close this out shortly...
0
 
sirbountyAuthor Commented:
Thanx for all your help, time & patience.  I've increased to 500 and split.
I appreciate the assistance.  

And if interested, I will probably be posting more questions today as I try to clean up this mess that I've made. :D
Thanx again!
0
 
fds_fatboyCommented:
Sorry for my ranting - I was getting a bit frustrated - especially as one of my colleagues was also pestering me with another nasty coding problem at the same time.
0
 
sirbountyAuthor Commented:
No prob sir.  
Have a good day. :)
0
 
jimbobmcgeeCommented:
>> No, that helps a LOT.  Networking/ANDing I understand.  Haha.  So VB's "OR" (bitwise compares) are like ANDing IP addresses...

Just to clarify

VB's AND is like ANDing IP addresses,
VB's OR is like ORing IP addresses (in theory, of course!!)

VB's bitwise AND multiplies each bit in one binary with its counterpart in another:  1 AND 0 = 0
VB's bitwise OR adds each bit in one binary to its counterpart in another:  1 OR 0 = 1
0
 
Erick37Commented:
To clarify a bit (no pun intended)

0100 Or 0100 = 0100

0100 And 0100 = 0100

0100 + 0100 = 1000 'the bit is carried

The Or operation is NOT like addition, and this is exactly why it should not be used when combining style bits.
0
 
jimbobmcgeeCommented:
>> The Or operation is NOT like addition, and this is exactly why it should not be used when combining style bits

You mean 'Addition should not be used when combining styles'?

So using OR prevents someone from doing vbQuestion + vbQuestion and getting a vbInformation box...?

because

     vbQuestion (32: 00100000)  +  vbQuestion (32: 00100000) = vbInformation (64: 01000000)
     vbQuestion (32: 00100000) OR vbQuestion (32: 00100000) = vbQuestion (32: 00100000)

?
0
 
Erick37Commented:
Right.
Using addition can result in overflow, or the wrong bits being set.
0
 
fds_fatboyCommented:
Now, we've learnt that who wants to show the logic behind a half-adder.

Yes - now do it purely with NAND gates ;-)
0
 
Erick37Commented:
Attempt #1 (brute force method)

Option Explicit

'
'Half Adder using only NAND gates
'
Private Sub Command1_Click()
   
    Dim B0 As Boolean, B1 As Boolean
    Dim bOut As Boolean, bCarry As Boolean
   
    'Set the values of Bit0 and Bit1
    B1 = (Check2.Value = vbChecked)
    B0 = (Check1.Value = vbChecked)
   
    'Run the half adder
    bOut = NAND(NAND(B1, NAND(B1, B0)), NAND(B0, NAND(B0, B1)))
    bCarry = NAND(NAND(B1, B0), NAND(B1, B0))
   
    'Display the results
    MsgBox "B0" & vbTab & "B1" & vbTab & "Out" & vbTab & "Carry" & vbCrLf & _
           B0 & vbTab & B1 & vbTab & bOut & vbTab & bCarry
   
End Sub

Private Function NAND(ByVal B1 As Boolean, ByVal B0 As Boolean) As Boolean
 
    NAND = Not (B1 And B0)
   
End Function
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.