Vba 将表格中的所有行提取为文本

Vba 将表格中的所有行提取为文本,vba,ms-access,Vba,Ms Access,我使用以下代码将表中的所有行提取到文本文件中。 我面临一个问题。如果该值为空,则获取该列的上一行值。 这是我的密码 Dim db As DAO.Database Dim rec As DAO.Recordset Dim strQry As String Dim aRow(1 To 16) As String Dim aBody() As String Dim lCnt As Long lCnt = 1 ReDim aBody(1 To lCnt) strQry = "SELECT * Fro

我使用以下代码将表中的所有行提取到文本文件中。 我面临一个问题。如果该值为空,则获取该列的上一行值。 这是我的密码

Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim strQry As String
Dim aRow(1 To 16) As String
Dim aBody() As String
Dim lCnt As Long

lCnt = 1
ReDim aBody(1 To lCnt)

strQry = "SELECT * From tbllast"
Set db = CurrentDb
Set rec = CurrentDb.OpenRecordset(strQry)

If Not (rec.BOF And rec.EOF) Then
    Do While Not rec.EOF
        lCnt = lCnt + 1
        ReDim Preserve aBody(1 To lCnt)                       
        aRow(1) = rec("cust_id")                        
        aRow(2) = Format(rec("app_date"), "yyyy/mm/dd")  
        aRow(3) = rec("work_id")                         
        aRow(4) = rec("app_price")                       
        aRow(5) = rec("doc_id")                          
        aRow(6) = rec("ass_id")                          
        aRow(7) = rec("pla_id")                          
        aRow(8) = NULL                             
        aRow(9) = NULL                            
        aRow(10) = rec("app_memo")              
        aRow(11) = rec("fin_price")                      
        aRow(12) = NULL                            
        aRow(13) = rec("pay_id")                         
        aRow(14) = rec("receipt")                        
        aRow(15) = rec("fin_memo")   
        aRow(16) = Format(rec("app_date"), "yyyy/mm/dd") 
        aBody(lCnt) = "('" & Join(aRow, "','") & "'),"
        rec.MoveNext
        Debug.Print aBody(lCnt)
    Loop
End If

aBody(lCnt) = aBody(lCnt)

Debug.Print Join(aBody, vbNewLine)
我在所有专栏中都面临着这个问题


任何想法。

在使用IsNull写入值之前,您应该检查该值是否为null,并可能为其分配一个带有“”的空字符串。您可以使用Nz功能来实现这一点:

aRow(1)=Nz(rec(“cust_id”).Value)

在所有可能为空的字段上使用Nz以返回空字符串:

aRow(1) = Nz(rec("cust_id").Value) 
... etc.