Solved

Msgbox in VB

Posted on 2004-09-24
10
1,504 Views
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

0
Comment
Question by:Vasi04
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 8

Assisted Solution

by:mladenovicz
mladenovicz earned 150 total points
ID: 12141077
1. it will be closed it self
2.3.
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

0
 
LVL 4

Accepted Solution

by:
Gruff82 earned 200 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)
        Text1.SetFocus
    End If
   
End Sub

Hope this helps
0
 
LVL 3

Assisted Solution

by:sgayatri
sgayatri earned 25 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.
ex:
vbinfo = MsgBox("Click any button", vbYesNoCancel, "Message")
If vbinfo = vbYes Then
  MsgBox "Yes"
'  do something
Else
  If vbinfo = vbNo Then
     MsgBox "No"
    'do something more
  Else
    MsgBox "Cancel"
   'do something else
  End If
End If

 For the remaining questions, the above two answers will suffice.
0
 
LVL 10

Assisted Solution

by:fds_fatboy
fds_fatboy earned 25 total points
ID: 12141211
Gruff82:
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
    Text1.SetFocus
End Sub
Private Sub Text2_LostFocus()
    If Len(Text2.Text) < 8 Then
        MsgBox ("Text2 less than 8 characters")
    End If
    Text2.SetFocus
End Sub

0
 
LVL 10

Expert Comment

by:fds_fatboy
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:Gruff82
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
0
 
LVL 18

Assisted Solution

by:JR2003
JR2003 earned 100 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
0
 

Author Comment

by:Vasi04
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
0
 
LVL 8

Expert Comment

by:mladenovicz
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
0
 
LVL 18

Expert Comment

by:JR2003
ID: 12142009
Vasi04
Looks like you need the GetWindowLong as mladenovicz pointed out (thanks mladenovicz)
JR
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

948 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

20 Experts available now in Live!

Get 1:1 Help Now