在 Access VBA 中从表中的记录创建查询
我正在使用 Microsoft Access 为小型企业进行产品定价。我有一个产品表、我们的供应商价格表和专门的客户表。有不同的查询会获取我们供应商的价格并将标记应用到不同的价格水平。对于某些客户,我们对某些产品给予特殊价格,但其余产品则属于一定的价格水平。然后,我将客户的特殊定价与其价格水平查询结合起来。如果存在特殊价格,它将使用该价格而不是常规价格水平的价格。为了获得此查询,我首先必须对每个客户进行查询,以从客户定价查询中获取他们的所有产品和特价。然后我将该查询与价格水平查询结合起来。如果我不执行第一步并加入带有价格水平查询的客户定价,则对同一产品有特殊定价的客户,该产品会多次显示,而我无法拥有。因此,如果我创建一个查询,从客户定价中取出他们的价格,然后加入价格水平,它就可以工作。
抱歉,如果我不清楚。如果您有疑问或想要了解如何设置的更多详细信息,我将提供。
我根本不懂VBA。我可以阅读并理解它,但从未编写过 VBA 代码。
我想要的是 VBA 代码来搜索客户表并检查每个客户是否有自己的查询将他们的价格与客户价格分开,因此如果添加新客户,它会自动为他们创建一个查询。然后创建一个查询,将其价格水平与刚刚创建的查询连接起来。
这可能吗。或者有没有更好的方法来解决这个问题。非常感谢。
这是到目前为止我的表格
供应商 产品 客户价格 客户
Prcode <--------Prcode 1-----许多 Prcode
名称 CustID 许多--------1 CustID
p; &nb CustPrice
的查询示例
以下是针对一个价格级别和一名客户J6
字段:PrCode名称 PrBulked Pr 多重$/加仑 $/单位
来源:所有这些都来自产品表“$/GAL:标记供应商$的计算字段”“$/UNIT:$/GAL * PrMultiple”
客户定价
字段:CustID客户名称 PrCode PrName 客户$
来源: CustPrds 海关 CustPrds Prds 客户订单
客户1
CustPricing 查询中 CustID=1 的所有字段
联接 Cust1 和 J6 联接 J6 中的所有记录以及 J6.PrCode=CustomerPricing.PrCode 的记录
PrCode - 来自 J6 的多个。 $/Gal:if CustID=1, cust$, J6.$/GAL
这是查询的 SQL
SELECT [Customer Products].customerid,
customers.customer,
[Customer Products].[Product Number],
chevron_products.[MATERIAL NAME],
chevron_products.bulked,
chevron_products.uom,
chevron_products.multiple,
[Customer Products].[Customer Price],
[Customer Price] * [Chevron_Products]![Multiple] AS [$/UNIT]
FROM customers
INNER JOIN ((chevron91311
RIGHT JOIN chevron_products
ON chevron91311.[MATERIAL NUMBER] =
chevron_products.[MATERIAL NUMBER])
INNER JOIN [Customer Products]
ON chevron_products.[MATERIAL NUMBER] =
[Customer Products].[Product Number])
ON customers.[Customer Number] = [Customer Products].customerid;
SELECT [Customer Pricing].customerid,
[Customer Pricing].[Product Number],
[Customer Pricing].[Customer Price]
FROM [Customer Pricing]
WHERE (( ( [Customer Pricing].customerid ) = 2 ));
SELECT j6.[MATERIAL NUMBER],
j6.[MATERIAL NAME],
j6.bulked,
j6.uom,
j6.multiple,
Iif([Customer Pricing].[CustomerID] = 2,
[Customer Pricing].[Customer Price], [J6].[$/GAL]) AS [$/GAL],
[$/GAL] * [J6].[Multiple]
AS [$/UNIT]
FROM j6
LEFT JOIN cobbprds
ON j6.[MATERIAL NUMBER] = cobbprds.[Product Number]
ORDER BY j6.[MATERIAL NAME];
I'm using Microsoft Access for product pricing for a small business. I have a products table, our supplier's prices, and specialized customers table. There are different queries that take our supplier's price and apply a mark up to different price levels. For some customers we give the a special price on certain products, but the rest of the products they belong to a certain price level. I then combine a customer's special pricing with their price level query. If a special price exists it uses that price instead of their regular price level price. To get this query I first have to make a query for each customer to get all of their products and special prices out of the customer pricing query. Then I join that query with the price level query. If I don't do the first step and join customer pricing w/ price level query, customers that have special pricing on the same product, that product shows up multiple times which I can't have. So if I create a query to take their prices out of customer pricing to begin with and then join with price level it works.
Sorry if I'm not clear. If you have question or want more detail how this is set up I will provide.
I don't know VBA at all. I can read it and follow, but never written VBA code.
What I want is VBA code to search the customers table and check if each customer has its own query that separates their prices from customer prices, so if a new customer is added it automatically creates a query for them. Then create a query that joins their price level with the query that was just created.
Is this possible. Or is there any better way to go about this. Thanks much.
Here's my tables so far
Supplier Products CustomerPrds Customers
Prcode <--------Prcode 1------many Prcode
Prname CustID many-------1 CustID
CustPrice
Here's an example of queries for one price level and one customer
J6
Field: PrCode PrName PrBulked PrMultiple $/GAL $/UNIT
Src: All of these are from Products tbl "$/GAL:calculated field to mark up supplier$" "$/UNIT:$/GAL * PrMultiple"
CustomerPricing
Field: CustID CustName PrCode PrName Cust$
Src: CustPrds Custms CustPrds Prds CustPrds
Cust1
All fields from CustPricing query where CustID=1
Joined Cust1 and J6 Join all records from J6 and records where J6.PrCode=CustomerPricing.PrCode
PrCode - Multiple from J6. $/Gal:if CustID=1, cust$, J6.$/GAL
Here is the SQL for the queries
SELECT [Customer Products].customerid,
customers.customer,
[Customer Products].[Product Number],
chevron_products.[MATERIAL NAME],
chevron_products.bulked,
chevron_products.uom,
chevron_products.multiple,
[Customer Products].[Customer Price],
[Customer Price] * [Chevron_Products]![Multiple] AS [$/UNIT]
FROM customers
INNER JOIN ((chevron91311
RIGHT JOIN chevron_products
ON chevron91311.[MATERIAL NUMBER] =
chevron_products.[MATERIAL NUMBER])
INNER JOIN [Customer Products]
ON chevron_products.[MATERIAL NUMBER] =
[Customer Products].[Product Number])
ON customers.[Customer Number] = [Customer Products].customerid;
SELECT [Customer Pricing].customerid,
[Customer Pricing].[Product Number],
[Customer Pricing].[Customer Price]
FROM [Customer Pricing]
WHERE (( ( [Customer Pricing].customerid ) = 2 ));
SELECT j6.[MATERIAL NUMBER],
j6.[MATERIAL NAME],
j6.bulked,
j6.uom,
j6.multiple,
Iif([Customer Pricing].[CustomerID] = 2,
[Customer Pricing].[Customer Price], [J6].[$/GAL]) AS [$/GAL],
[$/GAL] * [J6].[Multiple]
AS [$/UNIT]
FROM j6
LEFT JOIN cobbprds
ON j6.[MATERIAL NUMBER] = cobbprds.[Product Number]
ORDER BY j6.[MATERIAL NAME];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不完全确定我理解,但看起来一个快速的 VBA 条件语句就足够了...您可以使用 dlookup 来确定是否存在客户特定价格并根据结果设置查询。如果我偏离了基地 - 你能提供更详细的场景和示例数据吗?
下面是一些 VBA 代码,我将根据该客户/产品是否存在 CustPrice 在两个不同的查询之间进行选择...
现在,您已经有条件地将查询设置为执行您需要的操作,并且可以通过记录集执行:
顺便说一句 - 上面的 dlookup 与此查询相同。虽然我确信使用 dlookup 和记录集之间存在开销差异,但我不熟悉它们是什么。
Not completely sure I understand, but it looks like a quick VBA conditional statement would suffice... you can use dlookup to determine if there's a customer specific price and set your query based on the result. If I'm way off base - can you provide a more detailed scenario with sample data?
Here's some VBA code I would use to choose between two different queries based on the presence of a CustPrice for that customer/product...
Now you've conditionally set the query to do what you need it to and can execute through a recordset:
By the way - the dlookup above is the same as this query. While I'm sure there are overhead differences between using a dlookup and a recordset, I'm not familiar with what they are.