当字段名称是动态时vba制作表格

发布于 2024-07-10 02:45:20 字数 1066 浏览 6 评论 0原文

我有一个交叉表查询,它使用动态日期函数来生成列标题和字段名称。 例如:ForecastPeriod:DateAdd("m",[Period],[StartDate]) 这意味着每次运行交叉表查询时,我最终都会得到不同的字段名称。 我需要获取此交叉表的结果,并将其与其他 3 个类似交叉表的结果相结合以创建一个新表。 现在我使用一个 make table 查询和 3 个追加查询。 我这样做的原因是在预测期间的范围内为每个物料项目包含 4 个不同类别的数据。

结果如下所示:

材料类别 Per1 值 ...Per2 值 ...... Per24 值

MatA 需求 0 ... 10 ....... 0

MatA 需求美元 $0 ... $10 .... ... $0

MatA Forecast 10 ... 20 ....... 50

MatA Forecast Dollars $10 ... $20 ....... $50

问题是 make 表查询已经根据以下结果构建当前的交叉表查询。 当我下个月运行交叉表时,结果将具有不同的字段名称。 因此,我不得不手动更改 make 表查询设计中的周期,删除不再出现在结果中的周期并添加新的周期。

有没有办法使用 VBA 创建新表,直到交叉表运行后才知道字段名称?

或者有没有办法对字段名称进行编码或在运行后从交叉表中提取它们?

如果我使用如下代码: strSQL = "选择 tblForecast.Material, tblForecast.Category, tblForecast.X " & _ “INTO tblTemp” & _ “来自 tblForecast;”

我真的不知道 tblForecast.X 实际上会被称为什么。 可能是 11/1/08 或 12/1/08 等。

如果我声明一个变量来保存字段名称并使用日期代码来更改它,如何将其添加到表中? 我会使用 Alter Table 吗?

我确信这是可以做到的,我只是不知道如何去做,所以任何帮助将不胜感激! 谢谢!

I have a crosstab query that uses a dynamic date function to result in the column headers and therefore the field names. Ex: ForecastPeriod:DateAdd("m",[Period],[StartDate])
This means that every time I run the crosstab query, I could end up with different field names.
I need to take the results of this crosstab and combine it with the results of 3 other similar crosstabs to make a new table. Right now I use a make table query and 3 append queries. The reason I do this is to include 4 different categories of data per material item over the range of forecast periods.

Result looks something like this:

Material Category Per1 value ...Per2 value ...... Per24 value

MatA Demand 0 ... 10 ....... 0

MatA Demand Dollars $0 ... $10 ....... $0

MatA Forecast 10 ... 20 ....... 50

MatA Forecast Dollars $10 ... $20 ....... $50

The problem is that the make table query is built already against the results of the current crosstab query. When I run the crosstab next month, the results will have different field names. So I am stuck manually changing the periods in the make table query design, dropping the one no longer in the results and adding the new one.

Is there a way to use VBA to create a new table without knowing the field names until after the crosstab runs?

Or is there a way to code the field names or pull them from the crosstab after it runs?

If I use code like:
strSQL = "SELECT tblForecast.Material, tblForecast.Category, tblForecast.X " & _
"INTO tblTemp " & _
"FROM tblForecast;"

I really don't know what tblForecast.X will actually be called. It could be 11/1/08 or 12/1/08, etc.

If I declare a variable to hold the field name and use the date code to change it, how to I add it to the table? Would I have use Alter Table?

I'm sure this can be done, I just can't get my head around how to do it, so any help would be appreciated!! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞琼窗梦回ˉ 2024-07-17 02:45:20

VBA 创建镜像查询列的表:

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim qdf As DAO.QueryDef
Dim fld As DAO.Field
Dim SourceField As DAO.Field

Set db = CurrentDb

'Create a new tabledef
Set tdf = New DAO.TableDef

'Reference existing, saved querydef
Set qdf = db.QueryDefs("Query1")

tdf.Name = "Table2"

'iterate fields in the query and create fields in the new tabledef
For Each SourceField In qdf.Fields
    Set fld = tdf.CreateField(SourceField.Name, SourceField.Type, SourceField.Size)
    tdf.Fields.Append fld
Next

'Table is created and saved when the tabledef is appended to the current db
db.TableDefs.Append tdf

VBA to create a table mirroring the columns of a query:

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim qdf As DAO.QueryDef
Dim fld As DAO.Field
Dim SourceField As DAO.Field

Set db = CurrentDb

'Create a new tabledef
Set tdf = New DAO.TableDef

'Reference existing, saved querydef
Set qdf = db.QueryDefs("Query1")

tdf.Name = "Table2"

'iterate fields in the query and create fields in the new tabledef
For Each SourceField In qdf.Fields
    Set fld = tdf.CreateField(SourceField.Name, SourceField.Type, SourceField.Size)
    tdf.Fields.Append fld
Next

'Table is created and saved when the tabledef is appended to the current db
db.TableDefs.Append tdf
沉默的熊 2024-07-17 02:45:20

可以使用您自己的交叉表标题,例如:

ColHead:"Month" & DateDiff("m",[SaleDate],Forms!frmForm!txtSomeDate)

这将产生名为“Month”的列标题和 txtSomeDate 的偏移量。

还可以使用 vba 构建新查询。

It is possible to use your own headings for the crosstab, for example:

ColHead:"Month" & DateDiff("m",[SaleDate],Forms!frmForm!txtSomeDate)

This would result in column headings called 'Month' and an offset number from txtSomeDate.

It is also possible to use vba to build your new query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文