• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

to BergJC

It's a detail about my question:
"how to check a form input before saving?"
when user fill out all fields(text boxes)about Presonal Information then He/She will choose SavemnuItem under File menu.I thought if I disable Save Item when form is loaded, and after all fields are filled out eabled it. and give the user ability to save that information.I dontn't konw how to do it.
Regards,
          Kamijp
0
kamijp
Asked:
kamijp
  • 2
1 Solution
 
mark2150Commented:
Take the code I sent to your original question and enable/disable the SaveMnuItem on the basis of the Flag variable. Put the code in the KeyPress event for the from and set the form to KeyPreview True

M
0
 
BergJCCommented:
Place this code inside your MnuFile_click event (or whatever you called your file menu item)

Note: Instead of checking the length of the textboxes, check to make sure the trim$(Text1.Text) property is greater than spaces. If you only check the length, spaces would be allowed to pass through. If your text boxes are not in an array, just check them individually, like so:

Private Sub mnuFile_Click()

SaveMnuItem.Enabled = True
 
If Trim$(Text1.Text) <= "" Then
    SaveMnuItem.Enabled = False
End If

If Trim$(Text2.Text) <= "" Then
    SaveMnuItem.Enabled = False
End If

'and so on....

End Sub

If your textboxes are in an array, just loop through each one, using the same code I gave above. Try that, and if you need more help, just let me know.


0
 
kamijpAuthor Commented:
well, I tried what you both told me but it didn't work.I don't know why. so I sent you all my codes(I'm sure you can find lots of unuseful codes...I'm just tring diffrent ideas and options)
 they are not finish yet and I have to go thru them later.Sorry for the Mass!!
If you add any help or correction Please wirte it in BOLD to make it easy for me to undestand where you made the corrections.
Thanks in advance
                    Kami


Option Explicit

Private Sub mnuExitItme_Click()
    Unload Me
End Sub

Private Sub munOpenItem_Click()
 frmCalculator.Show 1
End Sub

Private Sub munSaveItem_Click()
Dim nFileNumber As Integer
Dim sMyData As String
CommonDialog1.CancelError = True
    On Error GoTo ErrHandler
CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files(*.txt)|*.txt"
CommonDialog1.ShowSave
nFileNumber = FreeFile
Open CommonDialog1.filename For Output As #nFileNumber
Write #nFileNumber, sMyData
 
ErrHandler:
  Exit Sub  'User pressed the Cancel button
End Sub

Private Function bInputIsValid(objControl As _
    Control, sMessage As String) As Boolean
    On Error GoTo ErrorHandler
    bInputIsValid = False
    Set objControl = txtLastName
    With objControl
      If Not IsString(.Text) Then
        sMessage = "Last Name must be a string."
        GoTo RoutineExit
      End If
      If .Text = "" Then
        sMessage = _
          "Please enter your Last name."
        GoTo RoutineExit
      End If
    End With
    Set objControl = txtName
    With objControl
      If Not IsString(.Text) Then
        sMessage = "Name must be a string."
        GoTo RoutineExit
      End If
      If .Text = "" Then
        sMessage = _
          "Please enter your name."
        GoTo RoutineExit
      End If
    End With
    Set objControl = txtAddress
    With objControl
      If Not IsString(.Text) Then
        sMessage = "Address must be a string."
        GoTo RoutineExit
      End If
      If .Text = "" Then
        sMessage = _
          "Please enter your Address."
        GoTo RoutineExit
      End If
    End With
    Set objControl = txtCity
    With objControl
      If Not IsString(.Text) Then
        sMessage = "City must be a string."
        GoTo RoutineExit
      End If
      If .Text = "" Then
        sMessage = _
          "Please enter your City."
        GoTo RoutineExit
      End If
    End With
    Set objControl = txtState
    With objControl
      If Not IsString(.Text) Then
        sMessage = "State must be a string."
        GoTo RoutineExit
      End If
      If .Text = "" Then
        sMessage = _
          "Please enter your State."
        GoTo RoutineExit
      End If
    End With
    Set objControl = txtZipCode
    With objControl
      If Not IsNumeric(.Text) Then
        sMessage = "Zip Code must be a number."
        GoTo RoutineExit
      End If
      If .Text <= 0 Then
        sMessage = _
          "Please enter your Zip Code."
        GoTo RoutineExit
      End If
    End With
    Set objControl = txtInvest
    With objControl
      If Not IsString(.Text) Then
        sMessage = "Investment Description must be a string."
        GoTo RoutineExit
      End If
      If .Text = "" Then
        sMessage = _
          "Please enter your Investment Description."
        GoTo RoutineExit
      End If
    End With
    bInputIsValid = True
   
RoutineExit:
  Exit Function
 
ErrorHandler:
  MsgBox Err.Description, vbCritical
  Resume RoutineExit
End Function

0
 
BergJCCommented:
First, you need to call the bInputIsValid function inside your mnuFile_click event. When you get an error and don't want the user to click on the SAVE menu, be sure to set mnuSaveItem.enabled = FALSE. Also remember to set it back to true right before you call bInputIsValid. One other note: when you are checking to see if a text box is empty, do it like this:

If Trim$(.Text) <= "" Then
     sMessage = _
     "Please enter your City."
     GoTo RoutineExit
End If


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now