Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    lzook88 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    75

    Updating Old Records Based on New Records

    I am attempting to code the VBA Submit button to add the items to the database (this works). Once the items are added, I want it to go back and look where those items came from and mark them used so we can show depleting stock.



    Here is the code:
    Code:
    Option Compare Database
    
    Private Sub Command61_Click()
    Dim DBSS As Database
    Dim rs As DAO.Recordset
    Dim from As String
    Dim prod As Variant
    Dim i As Integer
    Dim j As Integer
    
    Set DBSS = CurrentDb
    Set rs = CurrentDb.OpenRecordset("SELECT ProductName, OldLocation, NewLocation, Used FROM tblTransfers")
    
    
    
    'this part works!
    For i = 1 To 20
        If Me.Controls("item" & i) & "" = "" Then
            Exit For
        Else
            With rs
                For j = 1 To Me.Controls("quan" & i).Value
                    .AddNew
                    !ProductName = Me.Controls("item" & i).Value
                    !OldLocation = Me.txtFrom.Value
                    !NewLocation = Me.txtTo.Value
                    .Update
                Next j
            End With
        End If
    Next i
    
    rs.Close
    Set rs = Nothing
    
    
    
    'this part does not
    For i = 1 To 20
    prod = Me.Controls("item" & i).Value
    from = DLookup(prod, "tblTransfers", "NewLocation = '" & Me.txtFrom.Value & "'")
        If Me.Controls("item" & i) & "" = "" Then
            Exit For
        Else
            With rs
                For j = 1 To Me.Controls("quan" & i).Value
                    !Used = Yes
                    .Update
                Next j
            End With
        End If
    Next i
    
    
    
    
    
    
    
    DoCmd.Close
    End Sub
    I get an error with this code:

    Run-Time Error '3464':
    Data Type mismatch in criteria expression.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    It could be a couple things but my guess is your code is halting here
    !Used = Yes

    Is Used a Yes/No data type? Try ...
    !Used = True
    OR
    !Used = -1

  3. #3
    lzook88 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    75
    I updated the code to use 0 as used.. and when it throws the error and I hit debug it has this highlighted

    Code:
    from = DLookup(prod, "tblTransfers", "NewLocation = '" & Me.txtFrom.Value & "'")

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by lzook88 View Post
    I updated the code to use 0 as used..
    I do understand what you mean by this. As for the error in your DLookup, you might try something like ...

    Code:
    dim varFrom as variant
    dim strFrom as string
    
    strFrom = Me.txtFrom.Value
    debug.print strFrom
    
    varFrom = DLookup(prod, "tblTransfers", "NewLocation = '" & strFrom & "'")
    When it throws an error you can check the immediate window or hover over Me.txtFrom.Value to see what the value of this control is.



    .

  5. #5
    lzook88 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    75
    the first part instead of -1 I tried 0 as well.. my fault. fast typing and not paying attention.

    The second part:

    the varFrom still threw an error and hovering over it says VarFrom=Empty

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Correct, varFrom will be empty if the code halts on or before varFrom =

    What I believe is happening is there is an issue with the value of txtFrom. So you should be looking in the immediate window (Ctrl + G) and the following lines

    strFrom = Me.txtFrom.Value
    debug.print strFrom

  7. #7
    lzook88 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    75
    So here is what I am getting when hovering

    VarForm = Empty
    Prod = 11
    NewLocation = STN

    so the idea here would be that the DLookup would find the record matching Newlocation of STN with product of 11 (Bio-Hazard Bags) and change them from not used to used.


    but there could be multiple records that match. because the transaction table is set up that 1 record = 1 physical item

  8. #8
    DrGUI is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Parts Unknown
    Posts
    23
    In your DLookup statement, prod is a field in table tblTransfers, correct? If so, then try the following:

    from = DLookup("prod", "tblTransfers", "NewLocation = '" & Me.txtFrom.Value & "'")

    ***Note the quote marks around 'prod'

  9. #9
    DrGUI is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Parts Unknown
    Posts
    23
    Sorry about that. I just noticed that you calculate prod above it. However, the first param in the dLookup function must be of type string. If prod returns a string, then you need to surround it with quotes:

    from = DLookup("'" & prod & "'", "tblTransfers", "NewLocation = '" & Me.txtFrom.Value & "'")


    Question: you use dLookup to init the 'from' variable but you don't use it. Do you even need the dLookup statement?

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by DrGUI View Post
    Sorry about that. I just noticed that you calculate prod above it. However, the first param in the dLookup function must be of type string. If prod returns a string, then you need to surround it with quotes:

    from = DLookup("'" & prod & "'", "tblTransfers", "NewLocation = '" & Me.txtFrom.Value & "'")


    Question: you use dLookup to init the 'from' variable but you don't use it. Do you even need the dLookup statement?
    I did not bother to check MSDN but that simply looks all kinds of wrong.

  11. #11
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You have several errors.

    1) "FROM" is a reserved word in Access
    2) These two lines aren't used (not really an error but.... )
    Code:
    prod = Me.Controls("item" & i).Value
    from = DLookup(prod, "tblTransfers", "NewLocation = '" & Me.txtFrom.Value & "'")
    3) In the part that doesn't work, the "i" in this line should be a "j"
    Code:
                For j = 1 To Me.Controls("quan" & i).Value
    4) The biggest problem is that you closed the recordset "RS", destroyed the reference to it, then tried to reference it.
    Code:
    .
    .
    .
    rs.Close
    Set rs = Nothing
    
    'this part does not
    For i = 1 To 20
    prod = Me.Controls("item" & i).Value
    from = DLookup(prod, "tblTransfers", "NewLocation = '" & Me.txtFrom.Value & "'")
        If Me.Controls("item" & i) & "" = "" Then
            Exit For
        Else
            With rs
    .
    .
    .
    Looking at the code, if the part that doesn't work HAD worked, you would have randomly updated the "Used" field because you did not select a specific record to update.

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by lzook88 View Post
    So here is what I am getting when hovering

    VarForm = Empty
    Prod = 11
    NewLocation = STN

    so the idea here would be that the DLookup would find the record matching Newlocation of STN with product of 11 (Bio-Hazard Bags) and change them from not used to used.


    but there could be multiple records that match. because the transaction table is set up that 1 record = 1 physical item
    DLookup is not going to allow you to edit anything. So I am not sure why you went from a loop to a DLookup. You could either stick with DAO or build an SQL statement to UPDATE records, with a WHERE clause. Let me know. DAO should work OK. SQL may be an issue if you are using variables that change with a loop iteration.

  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I did not look at all of what Steve posted but if the code that you said works does not, you are going to need to address that issue first. I did not look at the code you said worked and Steve did.

  14. #14
    lzook88 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    75
    Guys I think I got everyone confused here.. Let me post the database for everyone to look at and see what you can figure out.

    The idea is that when the transfer form is used.. it will add the records (first part of the code) and then it will go back for the same records and see where they came from and mark them used (take them away from the current stock in the old location)....

    So I have marked the part of the code that works and what doesn't work inside the vba console in the database..

    thanks for all the help so far and further on..


    Zook

    Attachment 22057

  15. #15
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I will take a look at it. You nested loop looks OK at first glance.
    For j = 1 To Me.Controls("quan" & i).Value

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

Similar Threads

  1. Replies: 15
    Last Post: 08-07-2015, 10:46 AM
  2. Replies: 1
    Last Post: 06-17-2013, 11:44 AM
  3. Replies: 1
    Last Post: 02-16-2013, 11:36 AM
  4. Access 2007 - Creating New Records Based on Existing Records
    By GeorgeAugustinePhillips in forum Forms
    Replies: 9
    Last Post: 10-05-2011, 05:58 PM
  5. Replies: 12
    Last Post: 08-30-2011, 03:36 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