当字段名称是动态时vba制作表格
我有一个交叉表查询,它使用动态日期函数来生成列标题和字段名称。 例如: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
VBA 创建镜像查询列的表:
VBA to create a table mirroring the columns of a query:
可以使用您自己的交叉表标题,例如:
这将产生名为“Month”的列标题和 txtSomeDate 的偏移量。
还可以使用 vba 构建新查询。
It is possible to use your own headings for the crosstab, for example:
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.