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


Msgbox in VB

Posted on 2004-09-24
Medium Priority
Last Modified: 2008-03-17
1. How to close msgbox when user clicks "cancel" button?
2. How to convert text entered in textbox automatically converted to uppercase when the user leaves the textbox?
Suppose types "john" in the textbox (text1.text)
the display must be "JOHN"
3. I want to have a pop-up a msgbox when user enters less or more than 8 characters in the textbox, asking him to enter text 8 character long?
Thanks very much

Question by:Vasi04
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
  • 2
  • 2
  • 2
  • +3

Assisted Solution

mladenovicz earned 600 total points
ID: 12141077
1. it will be closed it self
Private Sub Text1_Validate(Cancel As Boolean)
    Text1.Text = UCase(Text1.Text)
    If Len(Text1.Text) <> 8 Then
        MsgBox "invalid length. must be 8"
        Cancel = True
    End If
End Sub


Accepted Solution

Gruff82 earned 800 total points
ID: 12141121
Hi Vasi04

What I would do is add the lost focus event handler for the textbox, so it get's called every time the user moves away from the text box.

I would then cause the text to become upercase and validate the length. If it isn't 8 charachters return the user to the text box to re-enter.

Here is the code:

' Event Handler gets called as user moves away
Private Sub Text1_LostFocus()
    ' make the text uppercase
    Text1.Text = UCase$(Text1.Text)

    ' Test the length to see if it is 8 charachters (trim off any trailing spaces)
    If Len(trim$(Text1.Text)) <> 8 Then
        ' Fails validation so select from the start to the end of text in text box and return focus
        MsgBox "You must enter 8 charachters", vbInformation + vbOKOnly
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
    End If
End Sub

Hope this helps

Assisted Solution

sgayatri earned 100 total points
ID: 12141179
What do yu mean by saying 'close msgbox when click cancel'
Clicking any button whether ok or cancel (or whatever way you display a msgbox ) will close yur msgbox
You can use the button information after a click.
vbinfo = MsgBox("Click any button", vbYesNoCancel, "Message")
If vbinfo = vbYes Then
  MsgBox "Yes"
'  do something
  If vbinfo = vbNo Then
     MsgBox "No"
    'do something more
    MsgBox "Cancel"
   'do something else
  End If
End If

 For the remaining questions, the above two answers will suffice.
Technology Partners: 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

Assisted Solution

fds_fatboy earned 100 total points
ID: 12141211
Validation on LostFocus gives problems. The Lostfocus event in general gives problems because it goes off after the next control has got focus and in certain circumstances, doesn't go off at all should only be used in moderation: For an example, try this:

Create a new standard VB project with a single form. Add the following 2 textboxes, Text1 and Text2; add a command button Command1.
Add the following code and run it. The When ti runs immediatey tab away from Text1 making it fail validation and see the results...

Option Explicit

Private Sub Command1_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Text1.Text = "Arry"
    Text2.Text = "Bert"
End Sub

Private Sub Text1_LostFocus()
    If Len(Text1.Text) < 8 Then
        MsgBox ("Text1 less than 8 characters")
    End If
End Sub
Private Sub Text2_LostFocus()
    If Len(Text2.Text) < 8 Then
        MsgBox ("Text2 less than 8 characters")
    End If
End Sub

LVL 10

Expert Comment

ID: 12141236
Also, another point about not using LostFocus event for validation is - your user can avoid validating a field simply by not visiting it. Using my previous example, run the program. Change the contents of Text1 to Arry1234 (valid - 8 characters) and click on the button. Hooray, passed validation - but the contents of Text2 are invalid.

Expert Comment

ID: 12141269
Your right about the lost focus event, probably best to use the validate event handler with cancel = true to force the field to be valid before moving on to the next one.

My mistake - thankfully its friday
LVL 18

Assisted Solution

JR2003 earned 400 total points
ID: 12141300
Use these functions in a module and call them with the textbox as a paramewter on the form load.

e.g to call it in the form load
TextBoxNumericOnly Text1

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Const ES_NUMBER = &H2000&
Private Const ES_UPPERCASE = &H8&
Private Const ES_LOWERCASE = &H10&
Private Const GWL_STYLE = (-16)

'Makes TextBox only receive numerics
Public Function TextBoxNumericOnly(TextBox As TextBox) As Boolean

    Dim iTmp As Long
    iTmp = SetWindowLong(TextBox.hwnd, GWL_STYLE, GetWindowLong(TextBox.hwnd, GWL_STYLE) Or ES_NUMBER)

End Function

'Makes TextBox only receive uppercase letters
Public Function TextBoxUpperCaseOnly(TextBox As TextBox) As Boolean

    Dim iTmp As Long
    iTmp = SetWindowLong(TextBox.hwnd, GWL_STYLE, GetWindowLong(TextBox.hwnd, GWL_STYLE) Or ES_UPPERCASE)

End Function

'Makes TextBox only receive Lowercase letters
Public Function TextBoxLowerCaseOnly(TextBox As TextBox) As Boolean

    Dim iTmp As Long
    iTmp = SetWindowLong(TextBox.hwnd, GWL_STYLE, GetWindowLong(TextBox.hwnd, GWL_STYLE) Or ES_LOWERCASE)

End Function

Author Comment

ID: 12141725
I have used codes from gruff82 and mladenovicz
Thanks all for your support.

I tried to use code from JR2003 for UPPERCASE. I could not get it compiled.
If you could explain how to incorporate this, it would be great!

Thanks all

Expert Comment

ID: 12141752
I think you need this line too for JR2003 code

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
LVL 18

Expert Comment

ID: 12142009
Looks like you need the GetWindowLong as mladenovicz pointed out (thanks mladenovicz)

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Suggested Courses

610 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