Results 1 to 4 of 4

Thread: [RESOLVED w/ ADO .fields} Mistake somewhere in my loop???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    78

    [RESOLVED w/ ADO .fields} Mistake somewhere in my loop???

    Thank you everyone for the suggestions received... played with the code and ended up using .fields instead of a command (works faster too!!!)

    With rs
    .fields("name") = .fields("name") / .fields("name")
    .update
    etc.
    etc.
    end with

    This works to calculate values in fields and put the answer into another field.



    ********************************
    Good afternoon,

    I am trying to calculate a value and put it into a field in an Access (2002) database via ADO and VB6. I had to create a loop to look at some fields first to determine if the calculation even needs to take place, then to figure out what field will be the basis for the calculation.

    I got this same calculation to perform earlier but it was from one specific field and all the fields were numeric... these are not!

    I cannot find my problem, however, I am new to this so goodness knows what silly thing I've done now. I have tried everything I can think of and am at my wits end, thus I'm begging (again!) for help from you wonderful gurus out here!!!!

    I have attached a Word document that contains the specific code if anyone has a minute to look it over and offer any suggestions!!!

    Thanks,
    Mary

    P.S. -- never mind, can't post Word....... converting to text......
    Attached Files Attached Files
    Last edited by A441OTA; Aug 21st, 2002 at 04:10 PM.

  2. #2
    Fanatic Member
    Join Date
    Nov 2001
    Location
    Bangkok
    Posts
    969

    Hi

    I am sure that many people are willing to have a look into your code BUT please format the code correct. It is absolutly impossible to read the code because it is all in 1 line. Send just the project itself without table, that is enough.


    Franky

  3. #3
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    You have to place all string values in between '' (single quotes)

    try it out .

    your code
    VB Code:
    1. 'Open ADO connection
    2. Dim SRT_DBcn As New ADODB.Connection
    3. cnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & "\SRT_DB.mdb;Persist Security Info=False"
    4. SRT_DBcn.Open cnString
    5.  
    6. Dim cmd As New ADODB.Command
    7. Dim rsSurvey As New ADODB.Recordset
    8. Dim SurveyRS As String
    9. Let SurveyRS = NewSurvey & "_QstData"
    10. 'Open Survey Question data table
    11. rsSurvey.Open SurveyRS, SRT_DBcn, adOpenKeyset, adLockOptimistic
    12. DoEvents
    13. With rsSurvey
    14.     .MoveFirst
    15.  
    16.     Do Until rsSurvey.EOF
    17.  
    18.         'Look at Scale_Label (text field), if "--" then it does not require a bar, if has text other than -- it requires a bar
    19.             If (rsSurvey!Scale_Label = "--") Then
    20.                 rsSurvey.MoveNext
    21.                 'If Scale_Label includes text other than -- it requires a bar
    22.             ElseIf (rsSurvey!Scale_Label <> "--") Then
    23.                 'Look at Continuous_Recode, if "NO" then bar_value needs to be calculated, otherwise the calculation was done earlier
    24.                 If (rsSurvey!Continuous_Recode = "NO") Then
    25.                     'if Response_1 is "--" and Response_2 has text then the calculation will be based on the value in "2" (text field)
    26.                     If (rsSurvey!Response_1 = "--") And (rsSurvey!Response_2 <> "--") Then
    27.                         cmd.CommandText = "UPDATE " & NewSurvey & "_QstData SET " & NewSurvey & "_QstData.Bar_Value = ROUND(val(" _
    28.                         & NewSurvey & "_QstData.2) - " & NewSurvey & "_QstData.Min_Val) / (" & NewSurvey & "_QstData.Max_Val - " _
    29.                         & NewSurvey & "_QstData.Min_Val)"
    30.                         cmd.CommandType = adCmdText
    31.                         SRT_DBcn.Execute cmd.CommandText
    32.                         'if Response_2 is "--" and Response_1 has text then the calculation will be based on the value in "1" (text field)
    33.                     ElseIf (rsSurvey!Response_2 = "--") And (rsSurvey!Response_1 <> "--") Then
    34.                         cmd.CommandText = "UPDATE " & NewSurvey & "_QstData SET " & NewSurvey & "_QstData.Bar_Value = ROUND(val(" _
    35.                         & NewSurvey & "_QstData.1) - " & NewSurvey & "_QstData.Min_Val) / (" & NewSurvey & "_QstData.Max_Val - " _
    36.                         & NewSurvey & "_QstData.Min_Val)"
    37.                         cmd.CommandType = adCmdText
    38.                         SRT_DBcn.Execute cmd.CommandText
    39.                         'if Response_2 is "--" and Response_1 is "--"  then the calculation will be based on the value in PR (text field)
    40.                     ElseIf (rsSurvey!Response_1 = "--") And (rsSurvey!Response_2 = "--") Then
    41.                         cmd.CommandText = "UPDATE " & NewSurvey & "_QstData SET " & NewSurvey & "_QstData.Bar_Value = ROUND(val(" & NewSurvey & "_QstData.PR) - " _
    42.                         & NewSurvey & "_QstData.Min_Val) / (" & NewSurvey & "_QstData.Max_Val - " & NewSurvey & "_QstData.Min_Val)"
    43.                         cmd.CommandType = adCmdText
    44.                         SRT_DBcn.Execute cmd.CommandText
    45.  
    46.                     End If 'end looking at 1, 2, and PR
    47.  
    48.                 End If  'end looking for all with Recode = "NO"
    49.  
    50.             End If ' end if for checking scale_label and BarValue for missing values - items without CR
    51.  
    52.         .MoveNext   'move to next record
    53.  
    54.     Loop
    55.  
    56. End With

    I don't know which datatype is used for which field so can do no more for the moment

    For calculations in your update statement you have to convert the non numeric fields.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    78
    Sorry Franky, I tried looking at the help to paste my code directly into the message so that it was readable, but didn't find the right info so I put it in a Word document and that wasn't accepted so I converted to text -- my apologies, I will make sure I do it right next time!! (The Word version looked great!!! LOL) Please forgive me.

    Thanks Swatty for the suggestions, I will go try them now... what do you mean "convert" -- does "val" not work in SQL? Or convert the field type? I will go do a search on Convert...

    Thanks both of you for taking the time to help -- I'll let you know how it turns out!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width