Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

excel - VBA: ADODB: get the values from RECSET.Fields

My code is posted below. I can't get the values in ranges "test2" and "test3". I have only the value in range "test". Do I need to modify the code ?

Thank you very much for your suggestions !

Public Sub INFO_PROTO(NO_POLICE As String)

Dim RECSET As New ADODB.Recordset
RECSET.Open "select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as b_perf_supp_ann, proto.b_perf_ctrat_gar as b_perf_ctrat_gar from db_dossier sousc, db_produit prod, db_protocole proto" & _
            " where sousc.no_police = '" & NO_POLICE & "' and sousc.cd_dossier = 'SOUSC' and sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit and sousc.is_protocole = proto.is_protocole ", cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
If Not RECSET.EOF Then
    Worksheets("1 - Feuille de Suivi Commercial").Range("test").Value = RECSET.Fields("b_perf_cma").Value
    Worksheets("1 - Feuille de Suivi Commercial").Range("test2").Value = RECSET.Fields("b_perf_supp_ann").Value
    Worksheets("1 - Feuille de Suivi Commercial").Range("test3").Value = RECSET.Fields("b_perf_ctrat_gar").Value
Else
   Worksheets("1 - Feuille de Suivi Commercial").Range("test").Value = "NC"
    Worksheets("1 - Feuille de Suivi Commercial").Range("test2").Value = "NC"
    Worksheets("1 - Feuille de Suivi Commercial").Range("test3").Value = "NC"
End If
RECSET.Close

End Sub

question from:https://stackoverflow.com/questions/65924830/vba-adodb-get-the-values-from-recset-fields

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this - it will replace any empty values with zeros

Public Sub INFO_PROTO(NO_POLICE As String)

    Dim RECSET As New ADODB.Recordset
    
    RECSET.Open "select proto.b_perf_cma as b_perf_cma, proto.b_perf_supp_ann as " & _
                " b_perf_supp_ann, proto.b_perf_ctrat_gar as b_perf_ctrat_gar " & _
                "from db_dossier sousc, db_produit prod, db_protocole proto" & _
                " where sousc.no_police = '" & NO_POLICE & "' and sousc.cd_dossier = 'SOUSC' " & _
               " and sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and " & _
              "sousc.is_produit = prod.is_produit and sousc.is_protocole = proto.is_protocole ", _
               cnn_Pegase, adOpenDynamic, adLockBatchOptimistic
    'use a With block to remove repetition
    With ThisWorkbook.Worksheets("1 - Feuille de Suivi Commercial")
    
        If Not RECSET.EOF Then
            .Range("test").Value = CheckValue(RECSET.Fields("b_perf_cma").Value)
            .Range("test2").Value = CheckValue(RECSET.Fields("b_perf_supp_ann").Value)
            .Range("test3").Value = CheckValue(RECSET.Fields("b_perf_ctrat_gar").Value)
        Else
            .Range("test").Value = "NC"
            .Range("test2").Value = "NC"
            .Range("test3").Value = "NC"
        End If
    
    End With
    RECSET.Close

End Sub

Function CheckValue(v)
    'Default to zero if null
    CheckValue = iif(Len(v) = 0, 0, v)
End Function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...