I have been using access as front end and SQL as Back end. I need help in generating an access report, by using a stored procedure with input parameters
as record source.
I tried the following code, but getting an error.Can't I use recordset as record source for the report. - Dim sDate As String
-
Dim eDate As String
-
Dim rsbatf As New ADODB.Recordset
-
-
sDate = InputBox("Enter Beginning Period", "Beginning Period")
-
eDate = InputBox("Enter Ending Period", "Ending Period")
-
-
Set GetProc = New ADODB.Command
-
cn.CursorLocation = adUseServer
-
Set GetProc.ActiveConnection = cn
-
GetProc.CommandType = adCmdStoredProc
-
GetProc.CommandText = "dbo.FreightCostAdjustments"
-
GetProc.Parameters("@startPeriod").Value = sDate
-
GetProc.Parameters("@endPeriod").Value = eDate
-
Set rsbatf = GetProc.Execute
-
Me.RecordSource = rsbatf
can anyone help please.
17 20779
GetProc doesn't seem to be defined.
I have never tried to set a recordsource based on a recordset.... It has been a long day and although I have a lot of experience in each app I don't have a lot of experience utilizing SQL server from MS Access. It would seem to me that you need a difinitive view or SQl statement from SQL server then you should be O.K. Forms
Forms and Reports in Access derive their data from queries or SELECT statements bound to the RecordSource property. In the client/server environment, the RecordSource property is based on a server-based view or on a stored procedure that is expected to return the same data as the original Access query. When you convert the record source, the same rules apply as for queries (see "Migrating Access Queries" earlier in this chapter).
http://www.microsoft.com/technet/pro....mspx?mfr=true
I have been using access as front end and SQL as Back end. I need help in generating an access report, by using a stored procedure with input parameters
as record source.
I tried the following code, but getting an error.Can't I use recordset as record source for the report. - Dim sDate As String
-
Dim eDate As String
-
Dim rsbatf As New ADODB.Recordset
-
-
sDate = InputBox("Enter Beginning Period", "Beginning Period")
-
eDate = InputBox("Enter Ending Period", "Ending Period")
-
-
Set GetProc = New ADODB.Command
-
cn.CursorLocation = adUseServer
-
Set GetProc.ActiveConnection = cn
-
GetProc.CommandType = adCmdStoredProc
-
GetProc.CommandText = "dbo.FreightCostAdjustments"
-
GetProc.Parameters("@startPeriod").Value = sDate
-
GetProc.Parameters("@endPeriod").Value = eDate
-
Set rsbatf = GetProc.Execute
-
Me.RecordSource = rsbatf
can anyone help please.
There is a lot to review before you even get to the Report aspect: __1. It is an accepted programming practice not to Declare and Instantiate an Object Variable on the same line: - Dim rsbatf As New ADODB.Recordset
-
'should be
-
Dim rsbatf As ADODB.Recordset
-
Set rsbatf As New ADODB.Recordset
-
-
Set GetProc = New ADODB.Command
-
'should be
-
Dim GetProc As ADODB.Command
-
Set GetProc = New ADODB.Command
__2. cn.CursorLocation = adUseServer. cn usually points to a Connection Object
and the Method to set up a Connection for this Command would be: - Set GetProc.ActiveConnection = CurrentProject.Connection
__3. Set GetProc.ActiveConnection = cn. cn should refer to a Connection Object - where? __4. GetProc.CursorLocation = adUseServer. CursorLocation is a Property of a Recordset Object and is used accordingly: - MyRS.CursorLocation = adUseServer
__5. You are passing Parameters (sDate and eDate) to a Stored Procedure which probably requires a Date Data Type, yet they are declared as Strings.
I have tried some of the changes you told. But I am still getting the same compilation error at the line "Me.RecordSource = rsbatf " as "Type Mismatch" by highlighting ".RecordSource".
Can't I put a recordset as RecordSource for the report ? There is a lot to review before you even get to the Report aspect: __1. It is an accepted programming practice not to Declare and Instantiate an Object Variable on the same line: - Dim rsbatf As New ADODB.Recordset
-
'should be
-
Dim rsbatf As ADODB.Recordset
-
Set rsbatf As New ADODB.Recordset
-
-
Set GetProc = New ADODB.Command
-
'should be
-
Dim GetProc As ADODB.Command
-
Set GetProc = New ADODB.Command
__2. cn.CursorLocation = adUseServer. cn usually points to a Connection Object
and the Method to set up a Connection for this Command would be: - Set GetProc.ActiveConnection = CurrentProject.Connection
__3. Set GetProc.ActiveConnection = cn. cn should refer to a Connection Object - where? __4. GetProc.CursorLocation = adUseServer. CursorLocation is a Property of a Recordset Object and is used accordingly: - MyRS.CursorLocation = adUseServer
__5. You are passing Parameters (sDate and eDate) to a Stored Procedure which probably requires a Date Data Type, yet they are declared as Strings.
NeoPa 32,496
Expert Mod 16PB
I have tried some of the changes you told. But I am still getting the same compilation error at the line "Me.RecordSource = rsbatf " as "Type Mismatch" by highlighting ".RecordSource".
Can't I put a recordset as RecordSource for the report ?
No.
RecordSource is the name of a record source (A string). It cannot be set to a non-string VBA program variable. It wouldn't make sense if you think about it.
Can't I put a recordset as RecordSource for the report ?
Something to look at: http://office.microsoft.com/en-us/ac...CH100621891033
Properties blocked by sandbox mode
In addition to the functions listed in the previous section, sandbox mode also blocks a number of object properties. The following table lists the objects and the blocked property or properties for each object.
Use the links in the See Also section for more help for the most commonly used properties. Object Blocked Property or Properties
BoundObjectFrame Object
Combobox Recordset
Control Object
CurrentProject AccessConnection, BaseConnectionString, CloseConnection, Connection, OpenConnection
CustomControl Object
Form Dynaset
Hyperlink Add to favorites
Listbox Recordset
ObjectFrame Object Report Recordset
SmartTagAction Execute
Screen ActiveDataAccessPage
Could be an issue...
Try reducing your Automation Security Level before setting the property. -
If Application.Version >= 11# Then
-
Application.AutomationSecurity = 1 ' msoAutomationSecurityLow
-
End If
I tried it this way an it works ok. - Private Sub Report_Open(Cancel As Integer)
-
Dim cmd As ADODB.Command
-
Set cmd = New ADODB.Command
-
-
cmd.ActiveConnection = Application.CurrentProject.Connection
-
cmd.CommandType = adCmdStoredProc
-
cmd.CommandText = "Name of the Store Procedure"
-
-
'Your parameters
-
cmd.Parameters("@startdate").Value = "1/1/1"
-
cmd.Parameters("@finaldate").Value = "7/7/7"
-
-
Set Me.Recordset = cmd.Execute
-
End Sub
I hope it helps...
I tried it this way an it works ok. - Private Sub Report_Open(Cancel As Integer)
-
Dim cmd As ADODB.Command
-
Set cmd = New ADODB.Command
-
-
cmd.ActiveConnection = Application.CurrentProject.Connection
-
cmd.CommandType = adCmdStoredProc
-
cmd.CommandText = "Name of the Store Procedure"
-
-
'Your parameters
-
cmd.Parameters("@startdate").Value = "1/1/1"
-
cmd.Parameters("@finaldate").Value = "7/7/7"
-
-
Set Me.Recordset = cmd.Execute
-
End Sub
I hope it helps...
In your workable code, the ActiveConnection Property of the Command Object is clearly defined as in: - cmd.ActiveConnection = Application.CurrentProject.Connection
whereas in your non-workable version: - Set GetProc.ActiveConnection = cn 'what does cn point to
try - Set GetProc.ActiveConnection = CurrentProject.Connection
Hi,
When I tried the statement
Me.Recordset = cmd.Execute
getting an error "THis feature is not available in an mdb".
But I can now able to get result into a record set(with your code). Thanks a lot for that. I am able to send the values into temporary variables but not into report controls.How do I get the values into report controls. Can you help me pls.
Thanks in advance In your workable code, the ActiveConnection Property of the Command Object is clearly defined as in: - cmd.ActiveConnection = Application.CurrentProject.Connection
whereas in your non-workable version: - Set GetProc.ActiveConnection = cn 'what does cn point to
try - Set GetProc.ActiveConnection = CurrentProject.Connection
Hi,
When I tried the statement
Me.Recordset = cmd.Execute
getting an error "THis feature is not available in an mdb".
But I can now able to get result into a record set(with your code). Thanks a lot for that. I am able to send the values into temporary variables but not into report controls.How do I get the values into report controls. Can you help me pls.
Thanks in advance
I performed a major Face Lift on the code and I think this will work. Let me know how you make out (Open() Event of Report). - Private Sub Report_Open(Cancel As Integer)
-
Dim sDate As String, eDate As String
-
-
sDate = InputBox("Enter Beginning Period", "Beginning Period")
-
eDate = InputBox("Enter Ending Period", "Ending Period")
-
-
'Cannot be Empty Strings or Nulls
-
If Len(sDate) = 0 Or Len(eDate) = 0 Then
-
Cancel = True
-
Exit Sub
-
End If
-
-
'Must be valid Dates
-
If Not IsDate(sDate) Or Not IsDate(eDate) Then
-
Cancel = True
-
Exit Sub
-
End If
-
-
Dim GetProc As ADODB.Command
-
Set GetProc = New ADODB.Command
-
-
With GetProc
-
'automatically maintained for *.adps
-
.ActiveConnection = CurrentProject.Connection
-
.CommandType = adCmdStoredProc
-
.CommandText = "dbo.FreightCostAdjustments"
-
.Parameters("@startPeriod").Value = sDate
-
.Parameters("@endPeriod").Value = eDate
-
End With
-
-
Set Me.Recordset = GetProc.Execute
-
Set GetProc = Nothing
-
End Sub
I am very thank ful to you.
I think the error might be the access problem, It likes the whole code except Set Me.Recordset = GetProc.Execute .
Getting the same error This feature is not available in an MDB, Run-time error '2593'. Can you figure out that.....please
Thanks again for spending your valuable time on this problem. I performed a major Face Lift on the code and I think this will work. Let me know how you make out (Open() Event of Report). - Private Sub Report_Open(Cancel As Integer)
-
Dim sDate As String, eDate As String
-
-
sDate = InputBox("Enter Beginning Period", "Beginning Period")
-
eDate = InputBox("Enter Ending Period", "Ending Period")
-
-
'Cannot be Empty Strings or Nulls
-
If Len(sDate) = 0 Or Len(eDate) = 0 Then
-
Cancel = True
-
Exit Sub
-
End If
-
-
'Must be valid Dates
-
If Not IsDate(sDate) Or Not IsDate(eDate) Then
-
Cancel = True
-
Exit Sub
-
End If
-
-
Dim GetProc As ADODB.Command
-
Set GetProc = New ADODB.Command
-
-
With GetProc
-
'automatically maintained for *.adps
-
.ActiveConnection = CurrentProject.Connection
-
.CommandType = adCmdStoredProc
-
.CommandText = "dbo.FreightCostAdjustments"
-
.Parameters("@startPeriod").Value = sDate
-
.Parameters("@endPeriod").Value = eDate
-
End With
-
-
Set Me.Recordset = GetProc.Execute
-
Set GetProc = Nothing
-
End Sub
I am very thank ful to you.
I think the error might be the access problem, It likes the whole code except Set Me.Recordset = GetProc.Execute .
Getting the same error This feature is not available in an MDB, Run-time error '2593'. Can you figure out that.....please
Thanks again for spending your valuable time on this problem.
I think that I may finally have the solution, if I am correct you are unfortunately in trouble unless you upgrade.
The capability of setting a Report's Recordset Property programmatically is NEW in Access 2002.
If you are using a Version of Access prior to 2002, you are in trouble. Let me know how you make out.
I am using access 2000. But if I kept a "record set" , instead of me.recordset like the following.
set rsbatf = GetProc.Execute
Now I can access the return values into variables of report in code.
But my problem is how do I get the values of variables into report controls(text boxes or labels).Is there any way to get the report variables into report controls.
Thanks I think that I may finally have the solution, if I am correct you are unfortunately in trouble unless you upgrade. If you are using a Version of Access prior to 2002, you are in trouble. Let me know how you make out.
I am using access 2000. But if I kept a "record set" , instead of me.recordset like the following.
set rsbatf = GetProc.Execute
Now I can access the return values into variables of report in code.
But my problem is how do I get the values of variables into report controls(text boxes or labels).Is there any way to get the report variables into report controls.
Thanks
Once a Recordset has been create via GetProc.Execute, you should be able to treat it like any other Recordset, namely: - Set rsbatf = GetProc.Execute
-
rsbatf.MoveLast: rsbatf.MoveFirst
-
-
Debug.Print rsbatf.RecordCount 'will give your the Number of Records
-
-
With rsbatf
-
Do While Not .EOF 'loop through all the Records
-
Debug.Print ![Field1]
-
Debug.Print ![Field2]
-
Debug.Print ![Field3]
-
Debug.Print ![...]
-
.MoveNext
-
Loop
-
End With
-
-
rsbatf.Close
But my problem is how do I get the values of variables into report controls(text boxes or labels).Is there any way to get the report variables into report controls.
Is your question how do I now populate the Fields (Labels and Text Boxes) in the Detail Section with appropriate values from the Recordset? If this is correct, then the mechanism to do this is probably very complex, which is probably why this capability didn't exist until Access 2002. An off-the-wall solution would be to add all the Records from the Recordset (rsbatf) to a Table which would then be the RecordSource for the Report. I think that I would hold off on this approach for awhile and see if someone comes up with a better solution. If you do want to take this unorthodox approach, I'll need the Field Names in the rsbatf Recordset as well as their Data Types and the Name of the Table which will be the RecordSource for the Report.
Yes, I did the same. I sent all the values into temporary variables and then used those values to populate labels on the report. But in this process it is not allowing textboxes to populate.
Until I get better solution, I will keep this code.
Thanks a lot for spending your valuble time on this problem. With out your code I may not able to get the record set. Once a Recordset has been create via GetProc.Execute, you should be able to treat it like any other Recordset, namely: - Set rsbatf = GetProc.Execute
-
rsbatf.MoveLast: rsbatf.MoveFirst
-
-
Debug.Print rsbatf.RecordCount 'will give your the Number of Records
-
-
With rsbatf
-
Do While Not .EOF 'loop through all the Records
-
Debug.Print ![Field1]
-
Debug.Print ![Field2]
-
Debug.Print ![Field3]
-
Debug.Print ![...]
-
.MoveNext
-
Loop
-
End With
-
-
rsbatf.Close
Is your question how do I now populate the Fields (Labels and Text Boxes) in the Detail Section with appropriate values from the Recordset? If this is correct, then the mechanism to do this is probably very complex, which is probably why this capability didn't exist until Access 2002. An off-the-wall solution would be to add all the Records from the Recordset (rsbatf) to a Table which would then be the RecordSource for the Report. I think that I would hold off on this approach for awhile and see if someone comes up with a better solution. If you do want to take this unorthodox approach, I'll need the Field Names in the rsbatf Recordset as well as their Data Types and the Name of the Table which will be the RecordSource for the Report.
Yes, I did the same. I sent all the values into temporary variables and then used those values to populate labels on the report. But in this process it is not allowing textboxes to populate.
Until I get better solution, I will keep this code.
Thanks a lot for spending your valuble time on this problem. With out your code I may not able to get the record set.
It's a shame that you can't upgrade to Access 2002, we wouldn't even be having this conversation.
Hi,
I'm trying to use the same functionality in my report.
I tried the code it works fine within Report_Open sub but when it comes to attach the controls(text boxes in the Detail Section) in the report with the recordset. It does not shows the records but a blank report. Is there something that needs to be done to attach to controls.
Thanks,
I tried it this way an it works ok. - Private Sub Report_Open(Cancel As Integer)
-
Dim cmd As ADODB.Command
-
Set cmd = New ADODB.Command
-
-
cmd.ActiveConnection = Application.CurrentProject.Connection
-
cmd.CommandType = adCmdStoredProc
-
cmd.CommandText = "Name of the Store Procedure"
-
-
'Your parameters
-
cmd.Parameters("@startdate").Value = "1/1/1"
-
cmd.Parameters("@finaldate").Value = "7/7/7"
-
-
Set Me.Recordset = cmd.Execute
-
End Sub
I hope it helps...
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
3 posts
views
Thread by kevin |
last post: by
|
4 posts
views
Thread by Magy |
last post: by
|
1 post
views
Thread by Robert |
last post: by
|
6 posts
views
Thread by Peter Neumaier |
last post: by
|
reply
views
Thread by benscribe |
last post: by
| |
6 posts
views
Thread by SethM |
last post: by
| |
1 post
views
Thread by leec |
last post: by
| | | | | | | | | | |