简体   繁体   English

SQL和MS访问-过滤表单上的数据

[英]SQL and MS access- filtering of the data on the form

I am using SQL 2005 as backend and MS Access as frontend. 我将SQL 2005用作后端,将MS Access用作前端。

Now, I get all my filtering of the data with views and no problems so far but I came accross some issues. 现在,我可以使用视图对数据进行所有筛选,到目前为止没有任何问题,但是遇到了一些问题。

In access I had a form and on that form I had a field1 which I used to filter the data I wanted in that form with query. 在访问中,我有一个表单,在该表单上,我有一个field1,用于过滤带有查询的该表单中所需的数据。 Example: Last, First Name or DOB. 示例:姓,名或DOB。 In Access I used the Expression builder to point the query to that field and I got my filter. 在Access中,我使用“表达式”构建器将查询指向该字段,然后得到了过滤器。 Now how do I do it in this new environment since when I create view (In Access) I can not filter on that field. 现在,如何在新环境中执行此操作,因为当我创建视图(在Access中)时,无法在该字段上进行过滤。

I was thinking on sp but I am not sure how do i go about it. 我在考虑sp,但不确定如何去做。

Any ideas? 有任何想法吗?

I was thinking on sp but I am not sure how do i go about it. 我在考虑sp,但不确定如何去做。

If you still wanted to keep this form a normal “bound” access form then you could do something like this. 如果您仍想将此表保留为普通的“绑定”访问表,则可以执行以下操作。

Setup a pass through query that fires your SP. 设置通过查询以触发您的SP。 The basic format for that is along the lines of this 的基本格式与此类似

EXEC [dbo].[spAgents_with_more_than_X_days_sick_by_Team] @Date_from = N'2009-09-14', @Date_to = N'2010-09-14', @Team_ID = N'TEM1', @Days_sick =5

You would then modify this when opening the form like this 然后,您可以在打开这样的表单时修改它

Set qDef = DBEngine(0)(0).QueryDefs("RqryAgents_more_than_X_sicks_detail_2")
With qDef
    .Connect = strSQL_con_string
    .SQL = "EXEC [dbo].[spAgents_with_more_than_X_days_sick_by_Team]"
    .SQL = .SQL & " @Date_from = N'" & Format(Me.txtDate_from, "yyyy-mm-dd") & "', "
    .SQL = .SQL & "@Date_to = N'" & Format(Me.txtDate_to, "yyyy-mm-dd") & "', "
    .SQL = .SQL & "@Team_ID = N'" & Me.txtTeam_ID & "', "
    .SQL = .SQL & "@Days_sick =" & Me.txtDays_sick
End With

This should work just fine however if it was me (and I know it's not everyone's preference) but I would make this an unbound form and populate it by firing your SP using ADO to fill a recordset and go from there. 这应该可以正常工作,但是如果是我(我知道不是所有人的喜好),但是我将使它成为未绑定的表单,并通过使用ADO填充SP并填充记录集并从那里开始来填充它。

If you want details of how to do that then just ask and I will post an example 如果您想知道执行该操作的详细信息,请询问,我将发布一个示例

EDIT: Code sample added 编辑:添加代码示例

Dim cmd as new ADODB.Command
Dim dbCon as new ADODB.Connection
Dim rst as new ADODB.Recordset
dbCon.ConnectionString=”Your_Connection_string”
dbCon.open

With cmd
    .ActiveConnection = dbCon
    .CommandText = "spYour SP"
    .CommandType = adCmdStoredProc
    .NamedParameters = True
    .Parameters.Append .CreateParameter("@Your_pram1", adVarChar, adParamInput, 20, Format(Me.txtDate, "yyyy-mm-dd"))
    .Parameters.Append .CreateParameter("@Your_pram2", adSmallInt, adParamInput, 0, Me.cboPhone_skill)
End With

Set rst = cmd.Execute()
With rst
    If .EOF=False then
    Me.txtYour_text_box_1=!Your_SP_field_1
    Me.txtYour_text_box_2=!Your_SP_field_3
    Me.txtYour_text_box_3=!Your_SP_field_2
    End if
End with
Rst.close
Dbcon.close
Set rst=nothing
Set cmd=nothing

Set dbcon=nothing 设置dbcon = nothing

You don't mention if these text boxes are in fact in the same form with the data and you don't mention if each text box was optional, and if they were not optional, then did you fill out 1, 2 or 3 boxes and then hit a button? 您没有提及这些文本框是否实际上与数据具有相同的格式,也没有提及每个文本框是否可选,如果它们不是可选的,那么您是否填写了1、2或3个框然后按下按钮?

If you just looking to find people with a given lastName, then in the after update event of the un bound lastname box, just go: 如果您只是想查找具有给定姓氏的人,那么在未绑定姓氏框的更新后事件中,只需执行以下操作:

Me.RecordSource = "select * from tblCustomers where LastName = '" & me.txtLastName "'"

The above is one line of code. 上面是一行代码。 I just don't see the need for so much code as others posted here. 我只是没有看到像这里其他人那样需要太多代码。

You can expand on your question if you looking to make all 3 boxes optional, or if you looking to have a search based on more then one box, but I can rather tell you that you don't need the amounts of code posted here, but the one line of code as per above will suffice. 如果您希望将所有3个框都设为可选框,或者希望基于一个以上的框进行搜索,则可以扩展您的问题,但是我想告诉您,您不需要此处张贴的代码量,但是上面的一行代码就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM