Solved

How do I declare vbYesNo?

Posted on 2004-09-29
39
1,518 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
  • 14
  • 10
  • 6
  • +4
39 Comments
 
LVL 10

Expert Comment

by:fds_fatboy
Comment Utility
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
Comment Utility
Dim blah As VbMsgBoxResult
Dim blah2 As VbMsgBoxStyle
0
 
LVL 31

Expert Comment

by:merowinger
Comment Utility
hi,


-vbAbortRetryIgnore
-vbApplicationModal
-vbCritical
-vbDefaultButton1
-vbDefaultButton2
-vbDefaultButton3
-vbDefaultButton4
-vbExclamation
-vbInformation
-vbMsgBoxHelpButton
-vbMSgBoxRight
-vbMsgBoxRtlReading
-vbMsgBoxSetForeground
-vbOKCancel
-vbOKOnly
-vbQuestion
-vbRetryCancel
-vbSystemModal
-vbYesNo
-vbYesNoCancel
0
 
LVL 10

Expert Comment

by:fds_fatboy
Comment Utility
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
Comment Utility
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
Comment Utility
Aha - I probably don't...brb
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
Comment Utility
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 200 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Bumping the points - thought this would be a simple one... :D
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Expert Comment

by:fds_fatboy
Comment Utility
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
Comment Utility
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
Comment Utility
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 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 150 total points
Comment Utility
>> 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 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No prob sir.  
Have a good day. :)
0
 
LVL 16

Expert Comment

by:jimbobmcgee
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
Right.
Using addition can result in overflow, or the wrong bits being set.
0
 
LVL 10

Expert Comment

by:fds_fatboy
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now