Results 1 to 7 of 7
  1. #1
    mcomp72 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    92

    Trying to clear a text box in a form

    Hi everyone. I'm attempting to clear a text box in a form, but obviously there's something I'm doing wrong. I've searched online, but can't find anything that helps.

    Here's what I'm trying to do: On my Timecard Form, I want to make sure that the user isn't creating a duplicate time card. So once they enter the job name (in a combo box), select the employee (also a combo box), and then the Week Ending date, I have written code for it to check if these three values already exist in any of the records of my Timecard Table. I'm doing this check on the BeforeUpdate on the WeekEnding text box on the form.

    Here's the code I've written for the Sub.



    Code:
    Private Sub WeekEnding_BeforeUpdate(Cancel As Integer)
    
    Dim rstT_Timecards As Recordset
    Dim JobID_Table As Integer
    Dim EmployeeOnJobsID_Table As Integer
    Dim WeekEndingDate_Table As Date
    Dim JobID_CurrentRecord As Integer
    Dim EmployeeOnJobsID_CurrentRecord As Integer
    
    Set rstT_Timecards = CurrentDb.OpenRecordset(Name:="T_Timecards", Type:=RecordsetTypeEnum.dbOpenSnapshot)
    
    JobID_CurrentRecord = Me.Controls("JobNameComboBox")
    EmployeeOnJobsID_CurrentRecord = Me.Controls("EmployeesOnJobsID")
    
    
    rstT_Timecards.MoveLast
    rstT_Timecards.MoveFirst
    
    With rstT_Timecards
    
        Do While Not .EOF
        
            If rstT_Timecards("TimecardID") <> Me.Controls("TimecardID") Then
        
                JobID_Table = .Fields("JobID_notFK")
                EmployeeOnJobsID_Table = .Fields("EmployeesOnJobsID")
                WeekEndingDate_Table = .Fields("WeekEnding")
                
                If JobID_Table = JobID_CurrentRecord And EmployeeOnJobsID_Table = EmployeeOnJobsID_CurrentRecord And Me.Controls("WeekEnding") = WeekEndingDate_Table Then
                    MsgBox "A time card for this Job/Person/Position already exists with this Week Ending date.  You cannot have more than one.  Please select another Week Ending Date."
                    Me.Controls("WeekEnding").Value = ""
                    Me.Refresh
                    rstT_Timecards.Close
                    Set rstT_Timecards = Nothing
                    Exit Sub
                End If
                
            End If
        
            rstT_Timecards.MoveNext
        
        Loop
    
    End With
    
    rstT_Timecards.Close
    Set rstT_Timecards = Nothing
    
    End Sub
    I am getting an error message on this line:
    Code:
    Me.Controls("WeekEnding").Value = ""
    I've tried replacing the "" with Null, but I still get the error.

    The WeekEnding field is of Data/Time format. Not sure if that matters.

    The error I'm getting is:
    Click image for larger version. 

Name:	2017-11-11 (1).png 
Views:	15 
Size:	6.8 KB 
ID:	31221

    I can tell I'm in over my head here. I haven't attempted to do any BeforeUpdate commands before, and obviously I'm doing something wrong.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    if the field is a datetime field, it will only accept null or a valid date value

    but your code is a lot more complex than it needs to be. you can just use

    Code:
    if dcount("*","T_Timecards","JobID_notFK=" & me.JobNameComboBox & " AND EmployeesOnJobsID=" & me.EmployeesOnJobsID & " AND WeekEnding=#" & format(me.WeekEnding,"mm/dd/yyyy") & "#")>0 Then
            MsgBox "A time card for this Job/Person/Position already exists with this Week Ending date.  You cannot have more than one.  Please select another Week Ending Date."
            me.WeekEnding = null
            me.weekending.setfocus
            cancel=true
    end if
    I can't work out what your criteria is supposed to be so it my best guess

  3. #3
    mcomp72 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    92
    So I can remove ALL of the code I am using and replace it with what you suggest, or your suggestion only replaces PART of my code?

    UPDATE: I decided to go ahead and replace the entire Sub with your code. So now I have:

    Code:
    Private Sub WeekEnding_BeforeUpdate(Cancel As Integer)
    
    If DCount("*", "T_Timecards", "JobID_notFK=" & Me.JobNameComboBox & " AND EmployeesOnJobsID=" & Me.EmployeesOnJobsID & " AND WeekEnding=#" & Format(Me.WeekEnding, "mm/dd/yyyy") & "#") > 0 Then
            MsgBox "A time card for this Job/Person/Position already exists with this Week Ending date.  You cannot have more than one.  Please select another Week Ending Date."
            Me.WeekEnding = Null
            Me.WeekEnding.SetFocus
            Cancel = True
    End If
    
    End Sub
    When I run it, I get the same error, but the runtime number is different.

    Click image for larger version. 

Name:	2017-11-11 (2).png 
Views:	14 
Size:	7.3 KB 
ID:	31222

    The line it is flagging is:
    Code:
    Me.WeekEnding = Null
    Do you think this could have something to do with it being a BeforeUpdate thing?

  4. #4
    mcomp72 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    92
    I just moved it from BeforeUpdate to AfterUpdate and I no longer receive the error. Any idea why this would be happening on BeforeUpdate? I am still too much of a novice with Access to have any idea.

    UPDATE: But now it doesn't work right. After I get the MsgBox, I then change the date to a different date, and sometimes it will give me the MsgBox after that, even though I am certain I have picked a new date that is not a duplicate.

    2ND UPDATE: I put my code back in (but still in AfterUpdate), and now it's working again.

  5. #5
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by mcomp72 View Post
    The line it is flagging is:
    Code:
    Me.WeekEnding = Null
    You cannot check for NULLs that way.
    A NULL is not equal to anything, not even another NULL.

    If you enter
    ? (1 = 1)
    in the immediate window, the result is TRUE.

    If you enter
    ? (Null = Null)
    in the immediate window, the result is Null (not TRUE as you might expect).


    You would have to use the IsNull() function:
    Code:
    If IsNull(Me.WeekEnding) Then
    Or you could use
    Code:
    If Len(Me.WeekEnding & "") > 0 Then

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by mcomp72 View Post

    I just moved it from BeforeUpdate to AfterUpdate and I no longer receive the error. Any idea why this would be happening on BeforeUpdate?
    You cannot change the value of a Control, in the BeforeUpdate event of that Control, because this interrupts the saving of the currently entered data of that Control...which is exactly what the error message tells you!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    ah - didn't notice that - thought it was the from beforeupdate event. My eyes aren't what they were

    if using the beforeupdate event of a control, you have to use the text property

    ...WeekEnding=#" & Format(Me.WeekEnding.text, "mm/dd/yyyy") & "#")...
    ...
    Me.WeekEnding.text = ""

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Save some text fields and clear others
    By whisp0214 in forum Forms
    Replies: 9
    Last Post: 10-25-2017, 03:48 PM
  2. Replies: 6
    Last Post: 01-15-2015, 05:41 PM
  3. Replies: 2
    Last Post: 03-12-2014, 01:31 AM
  4. Clear text box on form load
    By RoyLittle0 in forum Access
    Replies: 3
    Last Post: 03-05-2013, 05:40 AM
  5. Clear a form text box when user changes records
    By Artist.Anon in forum Forms
    Replies: 4
    Last Post: 08-19-2012, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums