Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    aquabp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    69

    Add a value to the underlying table field of a form using field on

    When I add a new record to my form I want to use the datefield to populate another field on that record using different formatting andadd that value to the underlying table. The problem is that I do not knowenough VBA to make it happen. The code below came from a previous question onthe forum. I am trying to adapt it to for this solution but no luck.

    Code:
    Private Sub MSort_Enter()
    'Code Purpose
                    'Change Date format to MSort Format “yymm”
                    'Add MSort Value to underlying Table
                    ‘Move to next field.
                    ‘Main purpose is to avoid keying mistakes. MSort is a monthly lookup value in forms and reports. 
    
    
    Dim rs As DAO.Recordset
    Dim RD As Integer
    If IsNull(Me.MSort) Then
        
        DoCmd.SetWarnings False
        'finds theMSort value
        'Set rs = CurrentDb.OpenRecordset("SELECT NRec FROM 350_IF01Q02")
        ' RD = Format(Date, "mmyy")
        'Set rs = CurrentDb.OpenRecordset("SELECT RDate FROM 300_ReceiptS")
        Set rs = currentdb.openrecordset("SELECT[RDate], Format([RDate],"yymm") AS MSort FROM 400_ReceiptS;")
                 'Line above does not work debug wants to put a list separator at yymm
    
        'Places the above MSort Value into the MSort Control
        Me.MSort = rs!MSort
        'Me.MSort = RD
        
        Me.VSort.SetFocus
    Else
        Me.VSort.SetFocus
    End If
    Any help would be greatly appreciated!




  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    This does not make sense because it is grabbing every record from the table and is only grabbing one column, NRec.
    Code:
    'Set rs = CurrentDb.OpenRecordset("SELECT NRec FROM 350_IF01Q02")
    If you want a specific field, use the field column name and the unique record. To get to the correct record, use the OpenRecordset method on an SQL statement that retrieves a single record or use the FindFirst method of the resulting recordset.
    Code:
    rs.findfirst "[KeyFieldName] =" & MyVariable
    MyOtherVariable = rs![AnotherColumnName]

  3. #3
    aquabp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    69
    Hi Itsme!
    From the first comment the parentheses means it is not used in the code, but this statement is pulling from a max value query that results in a single value. The problem is that the new record has not been updated to the table yet so the query does not find anything. The findfirst method is a search method right? Would I not have the same issue?

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    If 400_ReceiptS is a query that retrieves only one record, you can do something like this.
    Code:
    Set rs = currentdb.openrecordset("400_ReceiptS", dbopensnapshot)
    dim strResult as string
    strResult =  format(rs![RDate], "yymmm")
    msgbox strResult
    If RDate is not a of type Date, you will get an error on
    strResult = format(rs![RDate], "yymmm")

  5. #5
    aquabp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    69
    The 400_ReceiptS is a table that the 350_IF01Q02 queried and used in this line, Set rs = CurrentDb.OpenRecordset("SELECT NRec FROM 350_IF01Q02")
    Its been a frustrating afternoon.
    I tried playing around with 'strResult = rs.Fields.getValue("RDate") put the error message said method was not found. I also tried
    Set strResult = rs!db.OpenRecordset(RDate.Value)
    Which did not error out but I didn't know how to get it to yymm format and it did not put a value in MSort anyway.


    In a query you would create a field name: format(RDate,"yymm") and your done.
    We are entering data on a record. The first field manually keyed is the date field. Now we want it to convert to a MSort format using VBA so we do not get keying errors.
    This afternoon I was trying to grab the RDate value and assign it a var1 then assign Var1 to Var2 and format Var2 = format(Var1,"yymm"). From all I have read I am not sure I understand it but it sounded like it should work. I just do not understand how it would work. Is this off base?

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    What is the SQL of the query that retrieves the record you need?

  7. #7
    aquabp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    69
    Set rs = currentdb.openrecordset("SELECT[RDate], Format([RDate],"yymm") AS MSort FROM 400_ReceiptS;") but I get an error message and it is not specific to the current record. Even if I updated the record before entering the MSort Field I would have to get the Receipt value from the this record to query. Right? If we can do that then we should be able to get the RDate value, format it and deliver it to the MSort Field. Right?

  8. #8
    aquabp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    69
    I looked at the getvalue method this afternoon but when I put it in the code I get an error message say this method was not found. Is there a way to out put a value or a getvalue to a message to see if the value is actually being captured?

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    I am still curious if you have a query that retrieves a single record where the value of Rdate is the desired value. Can you post that SQL here?

  10. #10
    aquabp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    69
    No, I don't have a query. I would have to update the record to the table, then get the receipt value from this record to query this single record. Right?

    I use this in the default field in a form that I have open up from the notinlist event. =[Forms]![400_ReceiptF01]![400_ReceiptF03].[Form]![ISort].[Text]
    Can we capture the RDate using something like this? Can we assign something like this to a variable?

  11. #11
    aquabp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    69
    I tried this in the forms Default value for the MSort field.
    =IIf([MSort]=0,Format([Forms]![400_ReceiptF01]![400_ReceiptF02].[Form]![RDate],"yymm"),[MSort])
    IT did not work either.

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    It seems as though you are throwing everything except the kitchen sink at this. If you want to update a table with a value from another table, you are going to need to know which column and which row this value resides. You do not have to have a query. You could, maybe, use a DLookup() function.

    A query might look like
    SELECT RDate FROM 400_ReceiptS WHERE KeyFieldName = "SomeVariable"

  13. #13
    aquabp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    69
    Ok, I'll try that. To note I am trying to update a field from another field in the same table.

  14. #14
    aquabp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    69
    Hi ItsMe
    Ok, I couldn't get that to work. I tried to update the line so the sql statement could find it but could not get down to the single record without including the receipt field which is the primary field so I went and did the google to learn more about sql statements and variables. Long story short I came up with this code.
    Code:
    Dim RD As String
    Dim MS As Long
        
        If (Me.MSort) = 0 Then
        
            'finds the RDate for the MSORT field
            RD = Screen.ActiveForm.RDate
            MS = Format(RD, "yymm")
        
            'Places the above NRec into the GSort Control
            Me.MSort = MS
            DoCmd.GoToControl "VSort"
        Else
            Me.VSort.SetFocus
    
    
    End If
    It works but not sure if I am missing something that will haunt me in the future. This is not a multi user form yet. I'll cross that bridge one day I hope! If you could take a look at his and see if you see any issues I would appreciate it! Thanks again for your help!!

  15. #15
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by aquabp View Post
    Ok, I'll try that. To note I am trying to update a field from another field in the same table.
    Then, include that column in the Recordsource of the form.

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

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2015, 08:21 PM
  2. Replies: 4
    Last Post: 11-25-2014, 11:23 AM
  3. Replies: 12
    Last Post: 11-11-2014, 02:10 PM
  4. Replies: 5
    Last Post: 02-12-2014, 11:52 PM
  5. Form not updating underlying table
    By Kirsti in forum Forms
    Replies: 8
    Last Post: 02-10-2012, 12:23 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