DCount 与 SQL SELECT COUNT(*)?

Posted

技术标签:

【中文标题】DCount 与 SQL SELECT COUNT(*)?【英文标题】:DCount vs. SQL SELECT COUNT(*)? 【发布时间】:2009-06-18 19:54:51 【问题描述】:

我正在尝试根据分配给供应商的采购订单计算发送给供应商的所有物品的数量。但我似乎无法让控件根据采购订单实例显示许多项目 - 它不断抛出#name?或#错误!表单激活时文本框中的消息。

为 tblPODetail 创建语句

USE [instkeeper_test]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblPODetail](
    [intPurchaseOrderInstance] [int] IDENTITY(1,1) NOT NULL,
    [intPONumber] [int] NOT NULL,
    [datDateEntered] [datetime] NOT NULL,
    [bolPOOpen] [bit] NOT NULL,
    [intRMANumber] [nvarchar](255) NULL,
    [strSupplierCode] [nvarchar](50) NOT NULL,
    [strSupplierLastAddress] [nvarchar](50) NULL,
    [strSupplierLastCity] [nvarchar](50) NULL,
    [strSupplierLastState] [nvarchar](50) NULL,
    [strSupplierLastPostalCode] [nvarchar](50) NULL,
    [strSupplierLastCountry] [nvarchar](50) NULL,
    [strSupplierLastPhone] [nvarchar](50) NULL,
    [strSupplierLastFax] [nvarchar](50) NULL,
    [datDateSent] [datetime] NULL,
    [datDateReceived] [datetime] NULL,
    [bolAdvGageTrakNoTouch] [bit] NOT NULL,
    [bolAdvHardCopies] [bit] NOT NULL,
    [bolFreightShip] [bit] NOT NULL,
    [bolReturnableUsed] [bit] NOT NULL,
    [bolInHouse] [bit] NOT NULL,
    [strUPSTrackNumOut] [nvarchar](255) NULL,
    [strFedExTrackNumOut] [nvarchar](255) NULL,
    [strFreightTrackNumOut] [nvarchar](255) NULL,
    [strUPSTrackNumIn] [nvarchar](255) NULL,
    [strFedExTrackNumIn] [nvarchar](255) NULL,
    [strFreightTrackNumIn] [nvarchar](255) NULL,
    [SSMA_TimeStamp] [timestamp] NOT NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tblPODetail]  WITH CHECK ADD  CONSTRAINT [SSMA_CC$tblPODetail$strSupplierCode$disallow_zero_length] CHECK  ((len([strSupplierCode])>(0)))
GO

ALTER TABLE [dbo].[tblPODetail] CHECK CONSTRAINT [SSMA_CC$tblPODetail$strSupplierCode$disallow_zero_length]
GO

ALTER TABLE [dbo].[tblPODetail] ADD  CONSTRAINT [DF__tblPODeta__intPO__6C190EBB]  DEFAULT ((0)) FOR [intPONumber]
GO

ALTER TABLE [dbo].[tblPODetail] ADD  CONSTRAINT [DF__tblPODeta__datDa__6D0D32F4]  DEFAULT (getdate()) FOR [datDateEntered]
GO

ALTER TABLE [dbo].[tblPODetail] ADD  CONSTRAINT [DF__tblPODeta__bolPO__6E01572D]  DEFAULT ((1)) FOR [bolPOOpen]
GO

ALTER TABLE [dbo].[tblPODetail] ADD  CONSTRAINT [DF__tblPODeta__bolAd__6EF57B66]  DEFAULT ((1)) FOR [bolAdvGageTrakNoTouch]
GO

ALTER TABLE [dbo].[tblPODetail] ADD  CONSTRAINT [DF__tblPODeta__bolAd__6FE99F9F]  DEFAULT ((1)) FOR [bolAdvHardCopies]
GO

ALTER TABLE [dbo].[tblPODetail] ADD  CONSTRAINT [DF__tblPODeta__bolFr__70DDC3D8]  DEFAULT ((0)) FOR [bolFreightShip]
GO

ALTER TABLE [dbo].[tblPODetail] ADD  CONSTRAINT [DF__tblPODeta__bolRe__71D1E811]  DEFAULT ((0)) FOR [bolReturnableUsed]
GO

ALTER TABLE [dbo].[tblPODetail] ADD  CONSTRAINT [DF__tblPODeta__bolIn__72C60C4A]  DEFAULT ((0)) FOR [bolInHouse]
GO

为 tblPOGaugeDetail 创建语句

USE [instkeeper_test]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblPOGaugeDetail](
    [intItemPOID] [int] IDENTITY(1,1) NOT NULL,
    [intGagePOID] [int] NOT NULL,
    [strGageDetailID] [nvarchar](50) NULL,
    [datGageSent] [datetime] NULL,
    [datGageReceived] [datetime] NULL,
    [bolGageCalibrate] [bit] NOT NULL,
    [bolGageRepair] [bit] NOT NULL,
    [bolGageEvaluate] [bit] NOT NULL,
    [bolGageAccredited] [bit] NOT NULL,
    [bolGageReturned] [bit] NOT NULL,
    [bolGageException] [bit] NOT NULL,
    [bolGageExceptResolved] [bit] NOT NULL,
    [bolGageLeavePriceBlank] [bit] NOT NULL,
    [intGageCost] [real] NULL,
    [intTurnaroundDaysOut] [int] NULL,
    [SSMA_TimeStamp] [timestamp] NOT NULL,
 CONSTRAINT [tblPOGaugeDetail$PrimaryKey] PRIMARY KEY CLUSTERED 
(
    [intItemPOID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tblPOGaugeDetail]  WITH CHECK ADD  CONSTRAINT [tblPOGaugeDetail$tblPODetailtblPOGaugeDetail] FOREIGN KEY([intGagePOID])
REFERENCES [dbo].[tblPODetail] ([intPurchaseOrderInstance])
ON UPDATE CASCADE
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] CHECK CONSTRAINT [tblPOGaugeDetail$tblPODetailtblPOGaugeDetail]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [intGagePOID]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((1)) FOR [bolGageCalibrate]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [bolGageRepair]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [bolGageEvaluate]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [bolGageAccredited]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [bolGageReturned]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [bolGageException]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [bolGageExceptResolved]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [bolGageLeavePriceBlank]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [intGageCost]
GO

ALTER TABLE [dbo].[tblPOGaugeDetail] ADD  DEFAULT ((0)) FOR [intTurnaroundDaysOut]
GO

我希望计数出现在其中的采购订单详细信息的查询语句

SELECT dbo_tblPODetail.intPurchaseOrderInstance, dbo_tblPODetail.strSupplierCode, dbo_tblPODetail.bolPOOpen, dbo_tblPODetail.intPONumber, dbo_tblPODetail.datDateSent
FROM dbo_tblPODetail
WHERE (((dbo_tblPODetail.bolPOOpen)=True))
ORDER BY dbo_tblPODetail.datDateSent;

我有点沮丧,因为我已经阅读了一些关于 DCount() 和 SELECT COUNT(*) 的内容,但似乎无法让它们中的任何一个工作。任何帮助将不胜感激!提前致谢!

-- 编辑于 06/18/2009 @ 1542 小时--

这是我在子表单的 On Current 事件触发时尝试在代码中使用的 SELECT COUNT 示例:

SELECT COUNT(*) AS ItemsSent 
FROM [dbo_tblPOGaugeDetail] 
WHERE [dbo_tblPOGageDetail]![intGagePOID] = [dbo_tblPODetail]![intPurchaseOrderInstance]

这是附加到子窗体中的控件的 =DCOUNT 语句的示例。

=DCOUNT("*", "[dbo_tblPOGaugeDetail]", "[intGagePOID] = [Forms]![frmSupplierInfoMain]![subfrmOpenPOBySupplierID]![intPurchaseOrderInstance]")

这些都不起作用 - 很遗憾。

【问题讨论】:

您能告诉我们您尝试执行的选择计数吗? 我假设当您手动运行该选择查询时它可以工作。你是如何将结果输入到 TextBox 中的? 运行 SQL SELECT COUNT(*) 语句后,我会将其作为记录集拉取,将控件的值设置为该 DAO 记录集值。不过也没用。 【参考方案1】:

您的查询在没有计数(*)的情况下是否有效?

对 DCount 的第一个参数使用实际的字段名称 从 where 字符串中获取控件名称。

For DCOUNT("[intItemPOID],"[dbo_tblPOGaugeDetail]","[intGagePOID]=" & Forms]![frmSupplierInfoMain]![subfrmOpenPOBySupplierID]![intPurchaseOrderInstance])

【讨论】:

我的理解(这肯定是有缺陷的)我只能从使用 DCount 的域(表)中引用字段名称 - 在本例中为 dbo_tblPOGaugeDetail。我真的可以使用原始查询从中提取的 dbo_tblPODetail 表中的 intPuchaseOrderInstance 字段吗? 我得到一个#Name?当我在子窗体的控制源中尝试以下 DCount 时:=DCount([dbo_tblPOGaugeDetail]![intItemPOID],[dbo_tblPOGageDetail],[dbo_tblPOGaugeDetail]![intGagePOID]=[dbo_tblPODetail]![intPurchaseOrderInstance]) DCount 就像 SQL: "Field_Name", "Table_Name", "field_name = 'G'" 你需要字符串而不是表单上的对象(除非它们是一个字符串值,等于字段、表格、标准。 感谢您的帮助,我发现问题在于 Access 在使用表达式生成器时对其中一个表名使用的拼写。它在那里拼写错误,但在其他任何地方都没有 - 如果我手动编写它,它会起作用,如果我使用表达式生成器 - 它会爆炸。谢谢大家的帮助。 :)

以上是关于DCount 与 SQL SELECT COUNT(*)?的主要内容,如果未能解决你的问题,请参考以下文章

访问报表/SQL DCount 函数

Access VBA中的DCount

SQL重复记录查询-count与group by having结合查询重复记录

sql select__count.sql

sql select_count.sql

SQL - SELECT COUNT用法