• Trouble with code (log changes), form load order (A2k, 2k3)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Trouble with code (log changes), form load order (A2k, 2k3)

    Author
    Topic
    #427465

    Two questions:

    First error I receive is while modifying data on a tabbed form, the form’s focus is set after a listbox selection is made. I can make the changes in the data fields throught the form, as long as I don’t change the record. I can’t close the form, either. I receive the error: Run Time Error ‘2427’ You entered an expression that has no value.

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim db As DAO.Database
    Dim ctl As Control
    Dim rst As DAO.Recordset

    Set db = CurrentDb
    Set rst = db.OpenRecordset(“tblChanges”)
    ‘On Error GoTo Form_BeforeUpdate_Error

    For Each ctl In Me.Controls

    If TypeOf ctl Is Label Then
    ‘do nothing
    Else

    If Nz(ctl.OldValue) Nz(ctl.Value) Then
    With rst
    rst.AddNew
    rst!SSN = SSN
    rst!OBJECT_CHANGED = ctl.Name
    rst!prior_value = ctl.OldValue
    rst!current_value = ctl.Value
    rst!changed_by = GetNetUser()
    rst.Update
    End With
    End If
    End If

    Next ctl
    On Error GoTo 0
    rst.Close
    Exit Sub

    Form_BeforeUpdate_Error:
    If Err.Number = 2427 Then ‘this is the error number for something which does not have any data, and therefore does not have an “oldvalue”.
    Resume Next
    Else
    MsgBox “Error ” & Err.Number & ” (” & Err.Description & “) in procedure Form_BeforeUpdate”
    End If
    End Sub

    I assume it has something to do with the field having nothing in it to begin with (possibly Null?), and then me adding data, however I thought the Nz function would resolve that issue, and I still get the error when I modify the field with pre-existing data.

    Second question:
    When my form loads, there is a tab with a list box on it, and the list box’s SQL’s text box refers to [forms]![frmS1_IndividualInfo].txtSearch, a text box obviously that I use to filter the list box for a name. This works fine in A2k, however in 2k3 I receive a criteria prompt before the form even loads. What changed, and more importantly, how do I fix it? shrug

    Viewing 3 reply threads
    Author
    Replies
    • #990537

      Try testing for Null instead. The alternative would be to provide a 2nd argument for Nz, but that would entail testing the kind of value ctl can contain, not very attractive.

      • #990551

        It is a datasheet form, no lines, no buttons.

        Hans, something I forgot to mention (and should have, stupidme) is that I use this code on other forms, and it works flawlessly. brickwall

        When testing for null, I have tried
        _______________________
        If ctl.OldValue Is Null Then
        ‘do nothing
        Else
        _______________________

        For Each ctl In Me.Controls
        here
        If TypeOf ctl Is Label Then
        ‘do nothing
        Else
        and here
        If Nz(ctl.OldValue) Nz(ctl.Value) Then
        With rst
        rst.AddNew
        rst!SSN = SSN
        rst!OBJECT_CHANGED = ctl.Name
        rst!prior_value = ctl.OldValue
        rst!current_value = ctl.Value
        rst!changed_by = GetNetUser()
        rst.Update
        End With
        End If
        End If

        Each time, receiving the error ‘object required’. Where else would I test?

        • #990554

          It’s impossible to say without seeing the database.

          • #990555

            I was afraid you’d say that…oh boy… It’s going to take a while, but I’ll get it up soon – any ideas about my second question? I know you don’t use 2k3, but perhaps any suggestions?

        • #993766

          Jeremy, did you ever resolve this as I have just thought, as I look at it, has one of your table fields got a not null value in one of the fields?

    • #990546

      I’m suspicious of your…

      If TypeOf ctl Is Label Then
      ‘do nothing
      Else

      although I don’t have time to experiment. Seems to me you should be checking for several other control types that don’t have values as well (for instance, lines).

    • #990560

      Your form control
      [forms]![frmS1_IndividualInfo].txtSearch
      I notice you use a . between the form name and the control name, should you try using a ! instead? Just a guess.

    • #990624

      About your second question: I have now tried it in Access 2003 SP-1, and the problem didn’t occur. So for this one too, I think you’ll have to post a sample database.

      • #993771

        As requested, extremely stripped down, to the point of only showing the error with the applicable forms (btw, it took nearly 40 minutes to do!)

        • #993775

          What am I supposed to do? Most things I try cause messages about things missing from the database to be displayed.

          • #993935

            Hans,
            In A2k3, the only error I’m worried about is the criteria prompt that happens immediately upon opening the database.

            • #993965

              When I open the database (after converting) in Access 2003, the form frmS1 opens without criteria prompt or error. I do get an error message when I select an item in the list box, but that’s due to the stripping down of the database.

            • #993997

              brickwall AAAARGH!

              Any ideas why I would get one? I’m happy that it works fine for you, it means that what I’ve done works *somewhere*….just not where I need it to tongue

            • #993998

              Try changing the Row Source of lstNames to

              SELECT SSN, LNAME, FNAME, MI, Right([SSN],4) AS [LAST 4] FROM tblPersonnel WHERE LNAME Like Forms!frmS1!sfrmS1_IndividualInfo!txtSearch & “*” ORDER BY LNAME

              or even

              SELECT SSN, LNAME, FNAME, MI, Right([SSN],4) AS [LAST 4] FROM tblPersonnel WHERE LNAME Like txtSearch & “*” ORDER BY LNAME

            • #994003

              Hans,
              Those options stop the prompt, but then when I type a name into the text box, the list doesn’t filter.

            • #994004

              Since you now use the stored value of the text box instead of the actual text, you can’t use the On Change event of the text box any more. Use the After Update event instead. The user will have to tab out of the text box to make the list box update.

            • #994005

              Alternatively, change the row source of the list box in the On Change event of the text box:

              Private Sub txtSearch_Change()
              Me.lstNames.RowSource = "SELECT SSN, LNAME, FNAME, MI, " & _
              "Right([SSN],4) AS [LAST 4] FROM tblPersonnel WHERE " & _
              "LNAME Like '" & Me.txtSearch.Text & "*' ORDER BY LNAME"
              End Sub

            • #994006

              Hans, I changed

              SELECT SSN, LNAME, FNAME, MI, Right([SSN],4) AS [LAST 4] FROM tblPersonnel WHERE LNAME Like txtSearch & “*” ORDER BY LNAME

              to

              SELECT SSN, LNAME, FNAME, MI, Right([SSN],4) AS [LAST 4] FROM tblPersonnel WHERE LNAME Like txtSearch.text & “*” ORDER BY LNAME

              and it works fine. Thank you!

            • #994008

              And you don’t get an error message when you open the database?

            • #994021

              Nope. No error message.

            • #994022

              Let’s hope the problem is solved then… crossfingers

        • #993778

          Hi Jeremy I have had a quick look. Admittedly I have had to convert this to XP to view it.

          The unbound list box was not updating so I changed the SQL string in the data tab of the list box to:

          Like [forms]![frms1]![sfrms1_IndividualInfo]![txtSearch].[text] & “*” (note there is a dot between [txtSearch].[text])

          I also got rid of the after update event on list box (Private Sub lstNames_AfterUpdate())

          This appears to get the search up for Jezza Bear and Sergeant Hans V grin

          This has got rid of the horrible error message but am guessing if this is what you want?

          • #993784

            Jerry,
            Just did the same as you…still get the message brickwall

            What unbound list box?

            • #993788

              Code was:

              ‘Private Sub lstNames_AfterUpdate()
              ‘ Find the record that matches the control.
              ‘ Dim rs As Object

              ‘ Set rs = Me.Recordset.Clone
              ‘ rs.FindFirst “[SSN] = ‘” & Me![lstNames] & “‘”
              ‘ If Not rs.EOF Then Me.Bookmark = rs.Bookmark

              ‘Me!sfrmS1_IndividualInfo_Changes.Form!List65.Requery
              ‘End Sub

              sfrm below

            • #993823

              Well, the list requery at the end was for one of the subforms on another tab that I had to delete in order to compress the database. After you delete that code, does the form still find the record selected?

            • #993833

              No shrug

              It does with double click, so it obviously gets upset with after_update

              Private Sub lstNames_DblClick(Cancel As Integer)
              ‘ Find the record that matches the control.
              Dim rs As Object

              Set rs = Me.Recordset.Clone
              rs.FindFirst “[SSN] = ‘” & Me![lstNames] & “‘”
              If Not rs.EOF Then Me.Bookmark = rs.Bookmark

              End Sub

    Viewing 3 reply threads
    Reply To: Trouble with code (log changes), form load order (A2k, 2k3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: