MS Access-使用宏

时间:2019-07-11 14:21:34

标签: vba loops ms-access while-loop

我需要创建一个Access宏,该宏将从我的一张表中选择特定数量的行。

宏将每天运行,这需要选择特定数量的条目。对于“ t1”列中的每个条目,只需要从“样本大小”列中选择特定数量的行即可。如果数据库中没有任何条目,请跳过并转到下一个条目。

我的表格如下:

enter image description here

我试图使用下面的宏来定义一些要选择的请求行,这似乎是成功的。但是,我无法弄清楚如何将脚本的第一部分与另一部分链接以及如何对其进行修改以仅通过循环选择所需的结果(当前设置为选择前25名,但仅从第一部分中选择结果)我的列中的条目-“系统ANGEast”。我想我需要使用While Wend语句,但不确定在这种情况下如何使用它。

Sub DailyMacro()
Dim rs As DAO.Recordset
Dim tdf As TableDef
Dim fld As Field
Dim rst As Recordset
Dim strSQL As String
Dim strTableName As String

Set rs = CurrentDb.OpenRecordset("SELECT * FROM SampleF")

'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst 
Do Until rs.EOF = True

    'Save contact name into a variable
    Top = rs!Final_Daily_Sample_Size
    t2 = rs!t1

    'Move to the next record. Don't ever forget to do this.
    rs.MoveNext
Loop
Else
MsgBox "There are no records in the recordset."
End If
   rs.Close 'Close the recordset
   Set rs = Nothing 'Clean up

'Create a new temporary table containing the required fields

strSQL = "SELECT [30 Days Sample].[Account], [30 Days Sample].[Name], [30 Days Sample].[Last Name], [30 Days Sample].[Activity Date], [30 Days Sample].[Type], [30 Days Sample].[Returns], [30 Days Sample].[Amount], [30 Days Sample].[t1] " & _
"INTO [temp_table]" & _
"FROM [30 Days Sample];"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True

'Sort the data by the random number and move the top 25 into a new table

strTableName = "Final_Table" & Format(Date, "ddmmmyyyy")
strSQL = "SELECT TOP 25 [30 Days Sample].[Account], [30 Days Sample].[Name], [30 Days Sample].[Last Name], [30 Days Sample].[Activity Date], [30 Days Sample].[Type], [30 Days Sample].[Returns], [30 Days Sample].[Amount], [30 Days Sample].[t1] " & _
         "INTO " & strTableName & " " & _
         "FROM [temp_table]" & _
         "ORDER BY [temp_table].t1;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True

End Sub

任何帮助将不胜感激。预先感谢

0 个答案:

没有答案