Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Lin100
Yak Posting Veteran
70 Posts |
Posted - 2008-02-28 : 03:41:55
|
How write a VBA and stored procedure code that handle optional parameter.Access Version: 2002SQL Version : SQL 2000 ServerI have an ADP Access database and a form that has a combo box named Dept.When a user click on the Dept combo box; if the value is not null thenrun the stored procedure Get_SO_After_Dept_Update_1.If however, the value is null thenrun the stored procedure Get_SO_After_Dept_Update_2.Everything work very well. Now how do I go about to modified the VBA codeand the Stored Procedure code so that I will only have one storedprocedure to deal with instead of two. Another word, whether the value inthe Dept combo box is null or not null the VBA and the storedprocedure code will run.Combo Box Name : DeptControl Source :Row Source Type: Table/View/StoredProcRow Source :////////////////////////////////////////////////////////////////VBA Code in an Access ADP FormPrivate Sub Dept_AfterUpdate()Dim iDept As IntegerIf Not IsNull(Me.Dept) Then'Get_SO_After_Dept_Update_1 is a stored procedure that resides in SQL 2000 ServerMe.so.RowSource = "Exec [Get_SO_After_Dept_Update_1]'" & Me.Dept.Value & "'"Else'Get_SO_After_Dept_Update_2 is a stored procedure that resides in SQL 2000 ServerMe.so.RowSource = "Exec [Get_SO_After_Dept_Update_2]"End IfEnd Sub/////////////////////////////////////////////////////////////////Stored Procedure Get_SO_After_Dept_Update_1CREATE PROCEDURE Get_SO_After_Dept_Update_1@iDept intAS SELECT DISTINCT [1_Job - Parent].SONumber, Ref_DepartmentID.IDFROM dbo.[1_Job - Parent]INNER JOIN dbo.Ref_DepartmentIDON dbo.[1_Job - Parent].Department_Name = dbo.Ref_DepartmentID.DepartmentNameWHERE dbo.Ref_DepartmentID.ID = @iDeptGO/////////////////////////////////////////////////////////////////Stored Procedure Get_SO_After_Dept_Update_2CREATE PROCEDURE Get_SO_After_Dept_Update_2AS SELECT DISTINCT [1_Job - Parent].SONumber, Ref_DepartmentID.IDFROM dbo.[1_Job - Parent]INNER JOIN dbo.Ref_DepartmentIDON dbo.[1_Job - Parent].Department_Name = dbo.Ref_DepartmentID.DepartmentNameGO |
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-04 : 21:32:47
|
The value in the combo box in Access is not NULL, it is an empty string "" (two quotes)...there is a differenceYou can also just modify the SQL procedure to have a default parameter (one that would not appear in the combo box)in this case default = 0CREATE PROCEDURE Get_SO_After_Dept_Update_1 (@iDept int = 0)AS If @iDept >0BeginSELECT DISTINCT [1_Job - Parent].SONumber ,Ref_DepartmentID.IDFROM dbo.[1_Job - Parent] INNER JOIN dbo.Ref_DepartmentIDON dbo.[1_Job - Parent].Department_Name = dbo.Ref_DepartmentID.DepartmentNameWHERE dbo.Ref_DepartmentID.ID = @iDeptEndIf @iDept = 0BeginSELECT DISTINCT [1_Job - Parent].SONumber, Ref_DepartmentID.IDFROM dbo.[1_Job - Parent] INNER JOIN dbo.Ref_DepartmentIDON dbo.[1_Job - Parent].Department_Name = dbo.Ref_DepartmentID.DepartmentNameend Poor planning on your part does not constitute an emergency on my part. |
 |
|
Lin100
Yak Posting Veteran
70 Posts |
Posted - 2008-03-05 : 03:38:04
|
Thank you dataguru1971 for your help. |
 |
|
|
|
|