Results 1 to 5 of 5
  1. #1
    stalk is offline Competent Performer
    Windows 8 Access 2003
    Join Date
    May 2014
    Posts
    143

    I want to copy a date field values into 5 fields. How?

    I want to copy the date_tested field value into all the array_dt fields? mytbl.Fields(array_dt(i)).Value = mytbl.Fields(Date_Tested).Value



    Is the logic below correct. I 'm getting error message.
    Code:
    Public Const ct = "InA_Ct,InB_Ct,H1_Ct,Hx_Ct, RP_Ct"
    Public Const Dt = "InA_Date,InB_Date,H1_Date,Hx_Date,RP_Date"
    Public Const result = "InA_Result,InB_Result,H1_Result,Hx_Result,RP_Result"
     Sub arr_update()
        Dim objDB As DAO.Database
        Dim mytbl As DAO.Recordset
        Set objDB = CurrentDb()
        Set mytbl = objDB.OpenRecordset("Temp_RESULTS")
         Dim array_ct, array_dt, array_r, i
        Dim result_reported As String
        array_ct = Split(ct, ",")
        array_dt = Split(Dt, ",")
        array_r = Split(result, ",")
        For i = 0 To 4
        While Not mytbl.EOF
        mytbl.Edit
         mytbl.Fields(array_dt(i)).Value = mytbl.Fields(Date_Tested).Value
        If (mytbl.Fields(array_ct(i)).Value > 0 And mytbl.Fields(array_ct(i)).Value <= 32.99) Then
           mytbl.Fields(array_r(i)).Value = 1 'Positive
        ElseIf (mytbl.Fields(array_ct(i)).Value = 0) Then
           mytbl.Fields(array_r(i)).Value = 0 'Negative
        ElseIf mytbl.Fields(array_ct(i)).Value = Null Then
           mytbl.Fields(array_r(i)).Value = 9 'Not tested
            End If
        mytbl.Update
        mytbl.MoveNext
    Wend
    mytbl.MoveFirst 
    Next I
     End Sub

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Two issues: Dimming an Array and multiple dim's on a line.
    1) You Dim an Array with Dim array_ct() etc.
    2) From the second element on is Dimmed as a Variant unless specified.
    My guess is you really wanted:
    Dim array_ct()
    Dim array_dt()
    Dim array_r()
    Dim i as Integer

  3. #3
    stalk is offline Competent Performer
    Windows 8 Access 2003
    Join Date
    May 2014
    Posts
    143
    Dim is not the problem here. array_dt should be text field and date_tested is date field. all the Date fields I want should be in text data type. That is the error I'm getting .

    Can some one check the why I could not read Dat_tested value into my other ~_Date fields( InA_date, InB_date...)

    Quote Originally Posted by RuralGuy View Post
    Two issues: Dimming an Array and multiple dim's on a line.
    1) You Dim an Array with Dim array_ct() etc.
    2) From the second element on is Dimmed as a Variant unless specified.
    My guess is you really wanted:
    Dim array_ct()
    Dim array_dt()
    Dim array_r()
    Dim i as Integer

  4. #4
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    Always provide the error number/message and the line causing the error to minimize our guessing. I've never used .Value to set a field value, so I had to look it up. Found out it's the default property (that's why I've never used it) but I did come across this statement
    Trying to set or return the Value property in an inappropriate context (for example, the Value property of a Field object in the Fields collection of a TableDef object) will cause a trappable error.
    here: https://msdn.microsoft.com/EN-US/lib.../ff195493.aspx
    Your recordset seems to be based on a table, so I thought the statement there might be relevant.

    I don't see a problem with your Dim statement except for i - which you have created as a variant. The fact that you're using it as an integer or long may not be the problem. "array_dt" may be a text field somewhere (as you state), but in your code, it is an objectd variable of the variant type.
    Also, I have seen table recordset loops start in the middle of a table when MoveFirst was not executed, which you are not. Thus you might be trying to MoveNext (just before the Wend) when you are at EOF because your moves are being done inside a counter loop. You would be trying to move next regardless of the EOF. I would test for no records first (there's more than one way) as in
    Code:
    If Not (rs.BOF And rs.EOF) Then
      rs.MoveFirst
      **you can set or Dim or whatever, anything else. The counter goes in here. The beginning the inner loop:
    Do While Not rs.EOF
    ...more code
    Loop
    End If
    You could also entertain the idea of For i = 0 to Ubound(array_ct) as opposed to declaring constants. Should you add a field to this later, it's more work to modify your constants. Again, no idea of the error you're getting, so I'm left to guess.
    Last edited by Micron; 01-10-2018 at 09:24 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  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
    Your naming convention (or lack of one) makes it hard to decipher what is what.

    It looks like you have a table (or query) named "Temp_RESULTS" with at least 16 fields.
    For the fields "InA_Date", "InB_Date", "H1_Date", "Hx_Date", "RP_Date", are the field types "Date" or "Text" types?
    The field "Date_Tested" is a Date type field?
    Does the field "Date_Tested" have values (dates) in every record?


    If would really help if you would post your dB for analysis...



    There is another problem. You CANNOT write a test like this:
    Code:
                ElseIf mytbl.Fields(array_ct(i)).Value = Null Then
                    mytbl.Fields(array_r(i)).Value = 9    'Not tested
                End If
    The reason is this: Nothing is equal to NULL, not even Null.
    In the immediate window enter
    Code:
    ? 1=1
    then press enter. The result is TRUE
    Enter
    Code:
    ? Null = Null
    The result is FALSE.


    You have to use the function IsNull() or the function NZ():
    Code:
                ElseIf IsNull(mytbl.Fields(array_ct(i)).Value) Then
                    mytbl.Fields(array_r(i)).Value = 9    'Not tested
                End If
    Code:
                ElseIf NZ(mytbl.Fields(array_ct(i)).Value, 0) = 0 Then
                    mytbl.Fields(array_r(i)).Value = 9    'Not tested
                End If

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

Similar Threads

  1. Replies: 1
    Last Post: 03-07-2017, 06:32 AM
  2. Replies: 1
    Last Post: 11-21-2016, 06:48 AM
  3. Copy data from one field to several fields
    By Patsar in forum Queries
    Replies: 3
    Last Post: 10-03-2015, 09:20 PM
  4. Replies: 4
    Last Post: 07-15-2015, 12:42 PM
  5. Replies: 1
    Last Post: 10-29-2012, 08:15 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