VBA访问SQL查询问题

VBA访问SQL查询问题,sql,ms-access,vba,Sql,Ms Access,Vba,我需要您的帮助,我的SQL请求中有一个严重问题,它应该显示该服务的患者列表nepho,但它显示了所有患者的列表,因此我认为SQL查询根本不起作用 代码如下: Private Sub btnConnexion_Click() Dim Categ As Integer Dim Service As String Dim IdProf As Integer Dim db As DAO.Database Dim rs As DAO.Recordset Dim strSQL As String Se

我需要您的帮助,我的SQL请求中有一个严重问题,它应该显示该服务的患者列表
nepho
,但它显示了所有患者的列表,因此我认为SQL查询根本不起作用

代码如下:

Private Sub btnConnexion_Click()

Dim Categ As Integer
Dim Service As String
Dim IdProf As Integer

Dim db As DAO.Database
Dim rs As DAO.Recordset

Dim strSQL As String
Set db = CurrentDb


'vérification que l'utilisater a bien entrer e login et le mot de passe
Me.txtlogin.SetFocus
If IsNull(Me.txtlogin) Then
MsgBox "svp entrer votre login ", vbInformation, "login required "
Me.txtlogin.SetFocus
ElseIf IsNull(Me.txtmdp) Then
MsgBox "svp entrer votre mots de passe ", vbInformation, "mdp required "
Me.txtmdp.SetFocus
Else
'vérification que le login et le mdp sont corrects
If (IsNull(DLookup("login", "dbo_Authentification", "login='" & Me.txtlogin.Value & "'"))) Or _
    (IsNull(DLookup("mdp", "dbo_Authentification", "mdp='" & Me.txtmdp.Value & "'")))    Then
    MsgBox "login ou mdp incorrect"
Else
'récupération de l'IdCatégorie dans Categ, pour préciser les sessions des acteurs selon  leurs catégories professionneles
    Categ = DLookup("IdCategorie", "dbo_Professionnel", "IdProfessionnel = " &   DLookup("IdCompte", "dbo_Authentification", "login='" & Me.txtlogin.Value & "'"))
    'DoCmd.Close
    If Categ = 3 Then
        DoCmd.OpenForm "role"
        Else
        DoCmd.OpenForm "ListingPatients"

        'Service récupère le service du professionnel authentifié pour l'afficher à l'entete du formulaire "ListingPatients"
        Service = Nz(DLookup("IntituleServ", "dbo_Service", "IdService = " & DLookup("IdService", "dbo_Professionnel", "IdProfessionnel = " & DLookup("IdCompte", "dbo_Authentification", "login='" & Me.txtlogin.Value & "'"))), "inconnu")
        Forms![ListingPatients]![txtIntituleServ] = Service



        strSQL = "SELECT dbo_Patient.*, dbo_Service.IntituleServ, dbo_HospitalisatAcuelle.lit, dbo_Professionnel.IdProfessionnel, dbo_HospitalisatAcuelle.DateEntree, dbo_HospitalisatAcuelle.DateSortie FROM dbo_Service INNER JOIN ((dbo_Professionnel INNER JOIN dbo_Authentification ON dbo_Professionnel.IdProfessionnel = dbo_Authentification.IdCompte) INNER JOIN (dbo_Patient INNER JOIN (dbo_HospitalisatAcuelle INNER JOIN dbo_DonneePatientActuelles ON dbo_HospitalisatAcuelle.IdHosp = dbo_DonneePatientActuelles.IdHosp) ON dbo_Patient.IdPatient = dbo_DonneePatientActuelles.IdPatient) ON dbo_Professionnel.IdProfessionnel = dbo_HospitalisatAcuelle.IdProfessionnel) ON dbo_Service.IdService = dbo_Professionnel.Idservice WHERE (((dbo_HospitalisatAcuelle.DateEntree)<=Now()) AND ((dbo_HospitalisatAcuelle.DateSortie)>Now())) OR (((dbo_HospitalisatAcuelle.DateSortie) Is Null))"
        strSQL = strSQL & " AND [dbo_Service]![IntituleServ] = ' " & Service & " ' "


       Set rs = db.OpenRecordset(strSQL)

       rs.Close

       Set rs = Nothing
       Set db = Nothing
   End If
End If
End If

End Sub
Private Sub btnConnextion\u Click()
作为整数的Dim Categ
作为字符串的Dim服务
作为整数的Dim IdProf
Dim数据库作为DAO.Database
Dim rs作为DAO.Recordset
作为字符串的Dim strSQL
Set db=CurrentDb
“vérification que l”用于登录和通行证
Me.txtlogin.SetFocus
如果IsNull(Me.txtlogin),则
MsgBox“svp Enter votre登录”,vbInformation,“需要登录”
Me.txtlogin.SetFocus
如果ElseIf为null(Me.txtmdp),则
MsgBox“高级副总裁进货通知”,vbInformation,“需要mdp”
Me.txtmdp.SetFocus
其他的
“vérification que le login et le mdp sont更正了
如果(IsNull(DLookup(“login”、“dbo_authentication”、“login=””&Me.txtlogin.Value&“”)或_
(IsNull(DLookup(“mdp”、“dbo_身份验证”、“mdp='”&Me.txtmdp.Value&“”),然后
MsgBox“登录ou mdp不正确”
其他的
“我的猫是一种食物,是演员们的职业
Categ=DLookup(“IdCategorie”、“dbo_Professionnel”、“IdProfessionnel=”&DLookup(“IdCompte”、“dbo_身份验证”、“login=”””&Me.txtlogin.Value&“”)
“DoCmd.Close
如果类别=3,则
DoCmd.OpenForm“角色”
其他的
DoCmd.OpenForm“列出患者”
“专家服务”是指“列出患者”的处方药注册服务
Service=Nz(DLookup(“IntituleServ”、“dbo_Service”、“IdService=”&DLookup(“IdService”、“dbo_Professionnel”、“IdProfessionnel=”&DLookup(“IdCompte”、“dbo_authentication”、“login=”)、“inconu”)
形式![列出患者]![txtinituleserv]=服务
strSQL=“选择dbo_患者。*,dbo_服务.IntituleServ,dbo_hospitalistacuelle.lit,dbo_Professionnel.IdProfessionnel,dbo_hospitalistacuelle.dateentre,dbo_hospitalistacuelle.date来自dbo_服务内部连接的出动次数((dbo_Professionnel内部连接dbo_Professionnel.IdProfessionnel=dbo_authentication.IdCompte)内部连接(dbo_患者内部连接(dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo_医院内部连接dbo患者内部连接dbo_医院内部连接dbo患者内部连接dbo_医院内部连接dbo医院内部连接dbo患者内部连接DB在dbo_Service.IdService=dbo_Professionnel.IdService上,其中(((dbo_HospitalisatAcuelle.dateentre)Now())或((dbo_HospitalisatAcuelle.DateSortie)为Null))”
strSQL=strSQL&“和[dbo_服务]![IntituleServ]='”&服务&“
Set rs=db.OpenRecordset(strSQL)
rs.Close
设置rs=无
Set db=Nothing
如果结束
如果结束
如果结束
端接头

非常感谢您检查VBA记录集SQL查询的WHERE子句。您的括号集可能有问题。正如你所看到的dbo_服务![IntituleServ]字段未包含,但挂在OR语句上

考虑将DateSortie>=Now()之后的最后一个括号移动到DateSortie为Null之后

WHERE 

(
 (
   (dbo_HospitalisatAcuelle.DateEntree)<=Now()
 ) AND 

 (
   (dbo_HospitalisatAcuelle.DateSortie)>Now()
 )
) 

OR 
 (
  (
   (dbo_HospitalisatAcuelle.DateSortie) Is Null
  )
 )

AND 

[dbo_Service]![IntituleServ] = ' " & Service & " ' "
在哪里
(
(
(dbo_HospitalisatAcuelle.dateentre)Now()
)
) 
或
(
(
(dbo_HospitalisatAcuelle.DateSortie)为空
)
)
及
[dbo_服务]![IntituleServ]='“&服务&'”

数据库对象不支持查询,它只能用于打开整个表

相反,使用
QueryDef
对象。因此以下代码:

Set rs = db.OpenRecordset(strSQL)
应该是

'up at the top
Dim qdf as QueryDef
'... code

Set qdf = db.CreateQueryDef("",strSQL)
set rs = qdf.OpenRecordSet(DbOpenSnapshot)
'... at the bottom
qdf.close
此外,您的SQL语句中可能存在一个问题,即该部分应该位于何处

" WHERE (dbo_HospitalisatAcuelle.DateEntree<=Now() " & _
" AND (dbo_HospitalisatAcuelle.DateSortie>Now() OR dbo_HospitalisatAcuelle.DateSortie Is Null)) " & _
" AND [dbo_Service].[IntituleServ] = ' " & Service & " ' "
“其中(dbo_HospitalisatAcuelle.dateEnterenow()或dbo_HospitalisatAcuelle.DateSortie为空”)”&_
“和[dbo_服务].[IntituleServ]='”&服务&'”

(注意括号的位置)

在查询中生成服务名称时删除空格:使用
='“&Service&'”
而不是
='”&Service&'“
嗨,即使删除空格
和[dbo_Service]![IntituleServ]
和[dbo_Service]。[IntituleServ]谢谢您的回复,但它不起作用:(尝试在MS Access中使用strSQL的SQL定义创建一个查询对象,首先不使用,然后使用带有命名参数的final and子句。这将使您能够确保SQL查询在VBA例程中使用之前是正确的。strSQL=”从dbo_服务中选择dbo_Patient.*.............WHERE((dbo_Service.IntituleServ)='“&Service&'”)和((dbo_HospitalisatAcuelle.dateentre)Now())或((dbo_HospitalisatAcuelle.DateSortie)为Null));“这个请求是由图形界面使用WHERE…=“nephrono”!所以我所做的就是替换静态的“nerono”“通过变量”&Service&“但它仍然将所有列表“nepho+Cardio”!你有什么想法吗?你是否按照我的说明更改了括号?所有患者记录显示的原因可能是OR强制逻辑遵循以下任一条件:1)前两个条件(DateEntreNow)或2)后两个条件(DateSortie为Null,IntituleServ='nepho')。两者不在一起!是的,这是修改后的新WHERE:WHERE((dbo_HospitalisatAcuelle.dateentre)Now())或((dbo_HospitalisatAcuelle.DateSortie)为Null)和[dbo_Service]![IntituleServ]='“&Service&';“但仍然显示所有患者:/原谅我,但我看不到与前面WHERE子句的任何更改。我甚至返回到您的原始代码并进行了比较。只需将最后一个括号移到Null之前或-->之后。Null之后应该有3个括号。试试看….>Now())或((dbo_HospitalisatAcuelle.DateSortie)为Null));“不幸的是,问题仍然存在:(Hi@serakfalcon谢谢你的回复,我知道了错误