Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I declare vbYesNo?

Posted on 2004-09-29
39
Medium Priority
?
1,636 Views
Last Modified: 2008-01-09
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...
0
Comment
Question by:sirbounty
[X]
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
  • 14
  • 10
  • 6
  • +4
39 Comments
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12179179
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
 
LVL 4

Expert Comment

by:learning_t0_pr0gram
ID: 12179181
Dim blah As VbMsgBoxResult
Dim blah2 As VbMsgBoxStyle
0
 
LVL 31

Expert Comment

by:merowinger
ID: 12179191
hi,


-vbAbortRetryIgnore
-vbApplicationModal
-vbCritical
-vbDefaultButton1
-vbDefaultButton2
-vbDefaultButton3
-vbDefaultButton4
-vbExclamation
-vbInformation
-vbMsgBoxHelpButton
-vbMSgBoxRight
-vbMsgBoxRtlReading
-vbMsgBoxSetForeground
-vbOKCancel
-vbOKOnly
-vbQuestion
-vbRetryCancel
-vbSystemModal
-vbYesNo
-vbYesNoCancel
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12179202
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179216
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179219
Aha - I probably don't...brb
0
 
LVL 67

Author Comment

by:sirbounty
ID: 12179234
Well, on 2nd glance - why would I need that?  Isn't there a way to ref it without using VBA?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12179248
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179257
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
 
LVL 10

Accepted Solution

by:
fds_fatboy earned 800 total points
ID: 12179291
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12179324
>>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
 
LVL 32

Expert Comment

by:Erick37
ID: 12179366
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12179367
>> 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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179383
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12179406
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179414
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179442
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179466
Bumping the points - thought this would be a simple one... :D
0
 
LVL 67

Author Comment

by:sirbounty
ID: 12179486
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12179519
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12179536
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12179576
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
 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 600 total points
ID: 12179647
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179693
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12179722
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
 
LVL 32

Expert Comment

by:Erick37
ID: 12179756
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179764
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
 
LVL 16

Assisted Solution

by:jimbobmcgee
jimbobmcgee earned 600 total points
ID: 12179805
>> 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
 
LVL 16

Assisted Solution

by:jimbobmcgee
jimbobmcgee earned 600 total points
ID: 12179872
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179878
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179932
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
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12179955
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
 
LVL 67

Author Comment

by:sirbounty
ID: 12179994
No prob sir.  
Have a good day. :)
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12180062
>> 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
 
LVL 32

Expert Comment

by:Erick37
ID: 12180250
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12180575
>> 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
 
LVL 32

Expert Comment

by:Erick37
ID: 12180626
Right.
Using addition can result in overflow, or the wrong bits being set.
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12184911
Now, we've learnt that who wants to show the logic behind a half-adder.

Yes - now do it purely with NAND gates ;-)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12190476
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

Featured Post

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.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

604 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