我需要查询数据库表,然后将4个字段中的3个写入Excel电子表格。一旦写完该行,我需要将'sentback'字段更新为'Y'。这是where子句中使用的初始字段。所以整个想法是查询,写入和更改标记为'Y',所以我不再选择那些记录。
我有一半的代码在这里工作。我知道我可能会以某种方式使用DoCmd,但无法弄明白。
在例程运行期间可以将其他记录添加到数据库中,因此更新必须在do while中使用的当前记录集上进行,以避免在可能尚未发送的记录上设置标志。
非常感谢任何帮助
Dim strSQL As String
strExcelFile = "C:\MySpreadSheet.xls"
strWorksheet = "WorkSheet1"
strDB = "C:\track.accdb"
strTable = "manifests2"
Set db = CurrentDb
'If excel file already exists, you can delete it here
If Dir(strExcelFile) <> "" Then Kill strExcelFile
strSQL = "SELECT manifests2.[Shipment ID], manifests2.[Courier Service Level], manifests2.[Other Tracking],manifests2.[sentback] FROM " & "[" & strTable & "] WHERE (((manifests2.[sentback]) = 'N') and ((manifests2.[Other Tracking]) <> NULL))"
Set rs = db.OpenRecordset(strSQL)
Do While Not rs.EOF
rs.Edit
rs("sentback").Value = "Y"
rs.Update
rs.MoveNext
Loop
End Sub
答案 0 :(得分:0)
如果货件ID是唯一的标识符,您可以使用Excel文件更新表格,从而确保只标记文件中的记录。
strExcelFile = "C:\MySpreadSheet.xls"
'strWorksheet = "WorkSheet1"
'strDB = "C:\track.accdb"
strTable = "manifests2"
Set db = CurrentDb
''The sheet takes its name from the query. This connection string depends
''on the versions of Access and Excel that you are using.
cn = "[Excel 8.0;HDR=YES;IMEX=2;ACCDB=YES;DATABASE=" & strExcelFile & "].[OutDat$]"
''If excel file already exists, you can delete it here
If Dir(strExcelFile) <> "" Then Kill strExcelFile
''Just use an existing query.
''If a query is to be created/updated each time, you need to check
''if it exists and either create or update as appropriate.
''strSQL = "SELECT m.[Shipment ID], m.[Courier Service Level], " _
& "m.[Other Tracking], m.[sentback] " _
& "FROM [" & strTable & "] As m " _
& "WHERE m.[sentback] = 'N' And m.[Other Tracking] Is Not Null"
''db.CreateQueryDef "OutDat", strSQL
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "OutDat", strExcelFile
strSQL = "UPDATE [" & strTable & "] SET SentBack = 'Y' " _
& "WHERE [Shipment ID] In (SELECT [Shipment ID] FROM " _
& cn & ")"
db.Execute strSQL
有一些问题。例如,您在字段(列)名称中有空格。这将使你的生活变得更加困难。当YesNo(布尔值)是一个更明显的选择时,您似乎为sentback
选择了文本。没有任何东西等于或不等于null
,is null
或is not null
。