Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228

    Unhappy Last Part of code not working

    Hi guys.

    i Have written code for a program with the following goal:

    upon clicking the 'save button' it should check for null fields that are not supposed to be null , alert the user via message box that field is null, then set the focus on that field. after field is filled in, if the user clicks save again, it should then save the record, and call a function used to print the information just saved.

    problem is: when i hit save and all is filled it, the record is being saved twice, and my label is not printing the iformation just saved.
    it "prints" a blank label.



    i cannot figure out what is wrong. code below:

    Code:
    Private Sub btnsaverecord_click()
    Dim oktoclose As Boolean
    Dim strid As String
    Dim rs As DAO.Recordset
    
    
    Set rs = CurrentDb.OpenRecordset("Tbl_leadbond_yield", dbOpenDynaset)
    
    
    strid = ""
    
    
    oktoexit = False
    oktoexit = checkbeforesaving()
    
    
    If oktoexit = True Then
        Call updatevalues
        With rs
            .AddNew
            ![Dicing_lots_used] = Me.TxtDicingLots
            ![Date_Time] = Now()
            ![operator] = Me.CboOperator
            ![Work_Order] = Me.CboWorkorder
            ![total_leadbonded] = Me.TxtTotalLeadbonded
            ![Boats] = Me.txtBoats040
            ![Preform_Lot] = Me.CboPreformLot040
            ![Pin_Lot] = Me.CboPinLot040
            ![Lead_Lot] = Me.CboLeadLot040
            .Update
        End With
        rs.Close
        Call PrintLabels
        DoCmd.Close acForm, Me.Name
    End If
    End Sub
    update values only does some math. i dont think that is the problem.

    Code:
    Private Function checkbeforesaving()
    'this code checks for nullity of specific fields
    If IsNull(Me.TxtDicingLots) Then
    MsgBox "Please add dicing lots used."
    Me.TxtDicingLots.SetFocus
    checkbeforesaving = False
    ElseIf IsNull(Me.CboOperator) Then
    MsgBox "Please enter operator number."
    Me.CboOperator.SetFocus
    checkbeforesaving = False
    ElseIf IsNull(Me.CboPreformLot040) Then
    MsgBox "Please Enter Preform Lot information."
    Me.CboPreformLot040.SetFocus
    checkbeforesaving = False
    ElseIf IsNull(Me.CboPinLot040) Then
    MsgBox "Please enter Pin Lot number."
    Me.CboPinLot040.SetFocus
    checkbeforesaving = False
    ElseIf IsNull(Me.CboLeadLot040) Then
    MsgBox "please enter Lead Lot number."
    Me.CboLeadLot040.SetFocus
    checkbeforesaving = False
    ElseIf IsNull(Me.txtBoats040) Then
    MsgBox "Please enter number of boats built."
    Me.txtBoats040.SetFocus
    checkbeforesaving = False
    else checkbeforesaving=true
    endif
    end function
    Code:
    Private Function PrintLabels()      
          Dim Show_Box As Boolean
          Dim Response As Variant
          Dim i As Integer
          
          ' Set the Show_Dialog variable to True.
          Show_Box = True
    
    
          ' Begin While loop.
          While Show_Box = True
    
    
             ' Show the input box.
             Response = InputBox("Enter the number of labels to print or press Cancel to skip printing.", "Label Printing", 1)
    
    
             ' See if Cancel was pressed.
             If Response = "" Then
    
    
                ' If Cancel was pressed,
                ' break out of the loop.
                Show_Box = False
             Else
                ' Test Entry to find out if it is numeric.
                If IsNumeric(Response) = True Then
                    For i = 1 To Response
                        DoCmd.OpenReport "rpt_leadbond_yield_label", acViewPreview, , "[ID]=" & Me.ID
                    Next i
                   Show_Box = False
                Else
                   ' If the entry was wrong, show an error message.
                   MsgBox "Please Enter Numbers Only"
                End If
             End If
          ' End the While loop.
          Wend
    
    
    End Function
    heeeellllppppppppppppppppppppppppppppppp

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Is the form bound to the same table the code adds a record to? If so, that's the likely cause of your duplicate records, and would likely explain the labels problem too. You want a bound form or code, not both.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    thanks pbaldy,
    i saved everything using code, but when i used
    ![ID]=me.txtID

    i get an error in printlabels, where it calls out my ID to print that specific record

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Is ID an autonumber field? If so, you'd get the one added in code by adding this after the .Update line:

    .Bookmark = .LastModified
    lngARKey = !ARKey

    Replacing lngARKey with a variable of your own creation, and ARKey with the name of your field. Then use the variable in your printing code.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    Ok, it is saving only one record now, that is good.
    it is not giving an error anymore, another good thing.
    it is still printing blank records.

  6. #6
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    ID is an autonumber field, it is also my primary key
    what type of variable would lngARKey have to be? integer?

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It should be Long to match up to an autonumber field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    im sorry, that is not working for me, this is where i am at.
    my form is now unbound completely, and saving correctly.
    my ID field is also unbound on my form (and invisible) but it is saving nicely.
    i have reverted the code for
    Private Sub btnsaverecord_click() back to how it is shown in the original post.

    the error i get comes up with this line:

    DoCmd.OpenReport "rpt_leadbond_yield_label", acViewPreview, , "[ID]=" & Me.ID

    on my code to print labels.

    I'm assuming it does not know which ID to get.

  9. #9
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    the error given is: Run-time eorror '3075'
    syntax error (missing operator) in query expression '[ID]='.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you see this?

    Quote Originally Posted by pbaldy View Post
    use the variable in your printing code.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    PMFJI, but I saw a couple of things in your code...
    In "Sub btnsaverecord_click()", you have a variable named "oktoclose" (unused), but you use the undeclared variable "oktoexit" to terminate the loop.

    Do you have these two lines at the top of every code page?
    Option Compare Database
    Option Explicit



    I modified you code a little. If you grab the new record id and pass it to the print routine, it makes it a little easier.
    Code:
    Private Sub btnsaverecord_click()
    '   Dim oktoclose As Boolean
       Dim oktoexit As Boolean
       Dim strid As String
       Dim rs As DAO.Recordset
    
       Dim NewRecID As Long
    
       Set rs = CurrentDb.OpenRecordset("Tbl_leadbond_yield", dbOpenDynaset)
    
       strid = ""
    
       oktoexit = False
       oktoexit = checkbeforesaving()
    
       If oktoexit = True Then
          Call updatevalues
          With rs
             .AddNew
             ![Dicing_lots_used] = Me.TxtDicingLots
             ![Date_Time] = Now()
             ![Operator] = Me.CboOperator
             ![Work_Order] = Me.CboWorkorder
             ![total_leadbonded] = Me.TxtTotalLeadbonded
             ![Boats] = Me.txtBoats040
             ![Preform_Lot] = Me.CboPreformLot040
             ![Pin_Lot] = Me.CboPinLot040
             ![Lead_Lot] = Me.CboLeadLot040
             NewRecID = rs![ID]
             .Update
          End With
          rs.Close
          Call PrintLabels(NewRecID)
          DoCmd.Close acForm, Me.Name
       End If
    End Sub
    Code:
    Private Function PrintLabels(pRecID As Long)
       Dim Show_Box As Boolean
       Dim Response As Variant
       Dim i As Integer
    
       ' Set the Show_Dialog variable to True.
       Show_Box = True
    
       ' Begin While loop.
       While Show_Box = True
    
          ' Show the input box.
          Response = InputBox("Enter the number of labels to print or press Cancel to skip printing.", "Label Printing", 1)
    
          ' See if Cancel was pressed.
          If Response = "" Then
             ' If Cancel was pressed,
             ' break out of the loop.
             Show_Box = False
          Else
             ' Test Entry to find out if it is numeric.
             If IsNumeric(Response) = True Then
                For i = 1 To Response
                   DoCmd.OpenReport "rpt_leadbond_yield_label", acViewPreview, , "[ID]=" & pRecID
                Next i
                Show_Box = False
             Else
                ' If the entry was wrong, show an error message.
                MsgBox "Please Enter Numbers Only"
             End If
          End If
          ' End the While loop.
       Wend
    End Function

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Fair warning that your method of getting NewRecID doesn't work with linked SQL Server tables. That's why I use the method I posted; it works either way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by pbaldy View Post
    Fair warning that your method of getting NewRecID doesn't work with linked SQL Server tables. That's why I use the method I posted; it works either way.
    True... my bad for assuming an Access table. The idea was to pass the ID to the print function. Your method or using the "@Identity" property would still work.

  14. #14
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    hello guys,
    i actually tried both methods and they worked.
    Ssanfu, i hadn't realized i used the wrong variable, as you can see, they are similar in thought, unfortunately, VBA cannot tell what I am thinking. i used Pbaldy for linked SQL server tables.
    thanks so much for both of your help!
    -j

  15. #15
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Great. I didn't realize you were using SQL server tables.

    There is a setting in the VBA IDE - TOOLS / OPTIONS / Editor tab / "Require variable declaration" (check box) that will put "Option Explicit" at the top of every *new* code page that will (should) catch when you use undeclared variables.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 10-25-2011, 09:47 PM
  2. VBA Code not working how it Should
    By Juan4412 in forum Programming
    Replies: 7
    Last Post: 12-07-2010, 01:59 PM
  3. VBA Code Not working
    By jo15765 in forum Programming
    Replies: 12
    Last Post: 12-03-2010, 04:01 PM
  4. VB code not working
    By cwwaicw311 in forum Programming
    Replies: 17
    Last Post: 04-26-2010, 07:02 PM
  5. VBA Code for working with IE
    By smikkelsen in forum Programming
    Replies: 3
    Last Post: 04-15-2010, 01:05 AM

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