Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Oct 2003
    Location
    Finland
    Posts
    80

    Unanswered: Check empty fields!

    I need sub or function to check my pop-up form for empty fields because if the user leaves them empty my query won't run.

    I was thinking some kind of loop that checks the fields, but I don't know how loops work. The pop-up form contains two text fields and a combo box, that the code is supposed to check. Form also has two command buttons.

    I'm sure someone has some handy sub for doing this. If you have working piece of code, it might help me understand the structure of loops.

    Thanks in advance!

  2. #2
    Join Date
    Oct 2003
    Location
    Finland
    Posts
    80
    Btw... what is nz-function? Could I use it to change the value of the empty fields, so that the query could run?

  3. #3
    Join Date
    Jul 2003
    Location
    Amsterdam, Nederland
    Posts
    450

    Wink Hello

    Don't know why you need a loop to check a field


    if some 1 push a button then you chech the fields

    button1_click

    if me.txtbox1.text = "" then 'if the textbox is empty
    MsgBox "you must write a text here", vbOKOnly + vbInformation, "FieldName 1"
    me.txtbox1.setfocus
    exit sub
    endif

    if me.Combo1.Text ="" then
    MsgBox "you must select a value here", vbOKOnly + vbInformation, "ComboNeme 1"
    me.Combo1.setfocus
    exit sub
    endif

  4. #4
    Join Date
    Oct 2003
    Location
    Finland
    Posts
    80
    Ok, I'll try that...

  5. #5
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    if me.txtbox1.text = "" then 'if the textbox is empty
    MsgBox "you must write a text here", vbOKOnly + vbInformation, "FieldName 1"
    me.txtbox1.setfocus
    you must .SetFocus before reading .Text

    me.txtbox1.setfocus
    if me.txtbox1.text = ...etc

    izy
    currently using SS 2008R2

  6. #6
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1
    Originally posted by Sham81
    Ok, I'll try that...
    If you don't want to set the focus then use

    if txtbox1="" then
      Whatwever
    end if

    or

    if isnull(txtbox1) then
      Whatwever
    endif

  7. #7
    Join Date
    Oct 2003
    Location
    Finland
    Posts
    80
    How can I stop the query, if there's an empty field?

    And how can I make one message box, which gives all the field names that are empty.
    Like this for example:

    Fill the following fields:
    TextField 1
    TextField 2
    ComboBox 1

  8. #8
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    private sub yourButton_click()

    dim msg as string

    if isnull(NameOfControl1) then msg = msg & "NameOfControl1" & vbcrlf
    if isnull(NameOfControl2) then msg = msg & "NameOfControl2" & vbcrlf
    if isnull(NameOfControl3) then msg = msg & "NameOfControl3" & vbcrlf

    if isnull(msg) then
    'go ahead and do whatever the button is supposed to do
    else
    msg = "Missing value in:" & vbcrlf & msg
    msgbox msg, vbinformation, "Operator Error!"
    endif

    end sub

    izy

    oooops: forgot the quotes
    Last edited by izyrider; 11-24-03 at 07:04.
    currently using SS 2008R2

  9. #9
    Join Date
    Oct 2003
    Location
    Finland
    Posts
    80
    Thanks izy

  10. #10
    Join Date
    Nov 2003
    Posts
    1,487
    Dim Ctrl As Object, Strg As String
    For Each Ctrl In Me.Controls ' Iterate through each Control
    If IsNull(Ctrl) Then
    Strg = Strg & Ctrl.Name & vbNewLine
    End If
    Next
    MsgBox "Before You Go Any Further@@" & "The following fields require data:" & _
    vbNewLine & vbNewLine & Strg
    Last edited by CyberLynx; 12-03-03 at 01:53.

  11. #11
    Join Date
    Oct 2003
    Location
    Finland
    Posts
    80
    hmmm, there seems to be something wrong...

    Although all the fields are filled, I still get the error message box (it's empty). I created a msgbox to check the value of the msg-string (it was empty) so it should go to if IsNull(msg) Then, but it doesn't go there.

    code:

    Private Sub View_Click()

    On Error GoTo Err_View_Click

    Dim stDocName As String

    Dim msg As String

    If IsNull(Etunimi_popup) Then msg = msg & "Etunimi" & vbCrLf
    If IsNull(Sukunimi_popup) Then msg = msg & "Sukunimi" & vbCrLf
    If IsNull(Koulutusohjelma_popup) Then msg = msg & "Koulutusohjelma" & vbCrLf

    If IsNull(msg) Then
    stDocName = "Tulokset"
    DoCmd.OpenReport stDocName, acPreview
    DoCmd.Close acForm, "Haku"

    Else
    msg = "Täytä seuraavat kentät:" & vbCrLf & msg
    MsgBox msg, vbInformation, "Virhe!"

    End If

    Exit_View_Click:
    Exit Sub

    Err_View_Click:
    MsgBox Err.Description
    Resume Exit_View_Click

    End Sub

  12. #12
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    you are right - silly me.

    msg is not null at dim-time, it is empty.

    you need to check along the lines of
    if len(msg) = 0
    or
    if msg = ""

    also check out cyberlynx' suggestion: much more elegant if you are checking ALL controls on the form


    izy
    currently using SS 2008R2

  13. #13
    Join Date
    Oct 2003
    Location
    Finland
    Posts
    80
    Ok... and big thanks for everyone for helping me out

  14. #14
    Join Date
    Oct 2003
    Location
    Finland
    Posts
    80
    Oh, one more question: If I use that for each next-thingie, and my form has command buttons, do I need to leave them out somehow? They are controls too, right?

  15. #15
    Join Date
    Jul 2003
    Location
    Amsterdam, Nederland
    Posts
    450

    Cool use the typename()

    Originally posted by Sham81
    Oh, one more question: If I use that for each next-thingie, and my form has command buttons, do I need to leave them out somehow? They are controls too, right?

    Public Sub Controls_Set_Datasource(pForm As Form)
    'shearch controles on form
    Dim vControl As Control
    'On Error Resume Next

    For Each vControl In pForm.Controls
    Select Case TypeName(vControl)
    Case "TextBox", "DataCombo", "DTPicker", "MaskEdBox"
    If vControl.DataField <> "" Then
    Select Case TypeName(vControl)
    Case "TextBox"
    Set vControl.DataSource = pRecordset
    Case "MaskEdBox"
    Set vControl.DataSource = pRecordset
    Case "DataCombo"
    Set vControl.DataSource = pRecordset
    Case "DTPicker"
    Set vControl.DataSource = pRecordset
    End Select
    End If
    End Select
    Next
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •