Error opening report with a crosstab query - crosstab

I'm trying to open a report from a form, the report opens good when iI test it in MS Access but when I run it from my form (see the code below), it says
Run-time error '3070' the Microsoft Office Access database engine not recognize" as a valid field name or expression.
If Me.Check70.Value = True Then
DoCmd.OpenQuery "INTERMEDIATESYSTEM_CROSSTAB"
DoCmd.OpenReport "INTERMEDIATESYSTEM_Crosstab", acViewPreview
DoCmd.Close acQuery, "INTERMEDIATESYSTEM_CROSSTAB"
Application.DoCmd.SelectObject acReport, "INTERMEDIATESYSTEM_CROSSTAB"
DoCmd.Maximize
'DoCmd.Close acForm, "QBF_EDITFORM"
Exit Sub
End If
Like I said the cross tab query works good and the report works good but when I run it from the form it gives me an error.

Related

MS Access VBA DoCmd.OpenForm using where clause not filtering DAO query based recordset

I,m trying to get rid of linked tables and use only VBA code to generate recordset. I found that filtering data using where clause in my DoCmd.OpenForm command doesn't work this way. Is that expected behavior? Or maybe it should work and the problem is located somewhere else... Are OpenArgs the only thing that left me to do this?
To clarify my question:
I have two ms access forms:
One (continuous form) with hyperlink and on click code behind like follows
Private Sub txtPerson_Click()
DoCmd.OpenForm "frmPersonnelDetails", , , "PersonId = " & Me.txtPersonID, acFormReadOnly, acDialog
End Sub
and second one (frmPersonnelDetails), not connected to any recordsource, with recordset created with:
Private Sub Form_Load()
strQuery = "SELECT PersonID, Abbreviation, FirstName, LastName FROM SomeTable"
Set objDaoDb = GetDAODbConn 'function that returns database connection object
Set objDaoRS = objDaoDb.OpenRecordset(strQuery, dbOpenDynaset, dbSeeChanges)
Set Me.Form.Recordset = objDaoRS
End Sub
Now, where clause doesn't work. Second form is opening always on the first record. Is it normal? What is the best way to make it open on specified record?

Why is Access form giving me the error message "A column number has been specified more than once in the order list"? The related queries open fine

I'm working on an Access database with a SQL Server back end. Some times when I open a particular form, I get an Error 3146 "Column has been specified more than once in the order by list."
There are several possible queries that might feed into this form but I checked and ran them all individually and they run fine, so it's something in the form but I'm not sure. Below is a screen shot of the Data Properties window. My hypothesis is that "Order By" property (which I did not populate manually) is not being cleared out when the form closes, and thus when the form is re-opened, that in effect puts in the same columns twice.
I can temporarily solve the problem by opening up the form and clearing those Filter and Order By fields. That solves it for a while but intermittently it will happen again so there's probably one other thing I'm overlooking. I put in code in the Form close event to set the orderby="" but even that doesn't totally solve it.
Here is the code that opens the form from the main menu (frmProdSched is the form in question)
Private Function OpenProdSched(RunQuery, RunCaption)
DoCmd.OpenForm "frmProdSched", , RunQuery, "", , acNormal
Forms!frmProdSched.Caption = RunCaption
gScheduleRptQry = RunQuery
Me.Refresh
End Function
and then the code that is in the Form_Current event
Private Sub Form_Current()
On Error Resume Next
Dim strForm As String
Dim strForm1 As String
Dim strWhere As String
strForm = "frmSubmittalStatus"
strForm1 = "frmProdSched"
strWhere = "StatusJobno = " & Me.JOBNO & ""
If IsLoaded(strForm) Then
If Forms(strForm).Filter = strWhere Then
Forms(strForm1).SetFocus
Else
DoCmd.Close acForm, strForm
DoCmd.OpenForm FormName:=strForm, WhereCondition:=strWhere
Forms(strForm1).SetFocus
End If
End If
End Sub
I tried adding this code to the Form Close event and it seemed to work most of the time but it still intermittently happens
Private Sub Form_Close()
Me.Filter = ""
Me.OrderBy = ""
End Sub
I'm not sure what to check next. Any ideas?

How to explicitly save or close a recordset when a form deactivates?

I apologize if this is too vague a question, but we're having the following problem:
We use a search form to find a record, then load it in a bound-control form where changes are made. Then we return to the search form and open another record. When we do that, the form's BeforeUpdate property fires a 3020 error, "Update without Add New or Edit" and stepping through the code it's referring to the FIRST opened record. This is strange because there is no explicit update call, but after much trial and error I think the error is thus:
Record #1 is opened via the form and changes are made. Record #2 is opened on that same form without closing the first recordset. Even though we now re-opened the form with the second record, Access still assumes we're editing record 1, i.e. that we're trying to edit 2 records concurrently. Same as though we had a datasheet form and we edited one row and then tried to edit a second row without saving the first. What I want to be able to do is have it automatically do an update on the first record when the form deactivates so loading a new record doesn't cause this conflict.
So the bottom line is this: **Is there a way, on say the form's Dirty or Deactivate event, that we can force the form's recordset to update and close ** before loading a second record?
I hope I made this clear enough, it's a complex problem, so any small guidance would help. Btw, you may ask, "Why are you running the same code to open the same form twice?" Good question! Hey it's an old and badly written app (the thing has GoSubs in it for Pete's sake) but I have no choice but to make the best of a bad situation.
EDIT: I was asked to post code, which is reasonable, but it's in several different places. So I have the data form, it has a "Search" button to go back to the search form for opening another record. The search button on the data entry form is:
Private Sub CommandSearch_Click()
On Error GoTo Err_CommandSearch_Click
DoCmd.OpenForm "Reference Form", acNormal 'This is the form that does the searching
Exit_CommandSearch_Click:
Exit Sub
Err_CommandSearch_Click:
MsgBox Err.Description
Resume Exit_CommandSearch_Click
End Sub
When a record is selected on that search form, then the new form is opened with this code. Now this is where it gets tricky. I'm not the original programmer, as I said I think it was written in Access 97 by someone after taking an hour to read "Access for Dummies" :). But it always looks like only one copy of the form is open, so maybe it's re-opening it?
Public Sub CommandLoadCase_Click()
Dim LoadUTUCaseNumber As String, lengthUTUCaseNumber As Integer
lengthUTUCaseNumber = InStr(Forms![Reference Form]![Reference Query SubForm]![UTU Case Number], "-") - 1
If (lengthUTUCaseNumber = 0) Then
LoadUTUCaseNumber = ""
Else
LoadUTUCaseNumber = Left$(Forms![Reference Form]![Reference Query SubForm]![UTU Case Number], lengthUTUCaseNumber)
End If
On Error Resume Next
Forms![Case Appeal_Add-On Form].UTUCaseKeyToFind = LoadUTUCaseNumber
DoCmd.OpenForm "Case Appeal_Add-On Form", acNormal, , , , , LoadUTUCaseNumber
'Case Appeal Add On Form referred to here is the data entry form I refer to above.
End Sub
Finally, the Error 3020 (Update without Add/Edit) is occurring after it executes this line on the data entry form. (I know the code is complicated which is why I didn't enter it at first).
Private Sub Form_BeforeUpdate(Cancel As Integer)
[UTU Claim Sequence Key] = [UTU Claim Alpha Sequence] & CStr([UTU Claim Numeric Sequence])
End Sub

Create Access reports from Form and save as PDF

Im currently using an Access database to create alot of reports - the report is made using a Query as a source, in the Query i use a Form as criteria so that the open page in the Form is the data used in the report.
Then I save the report as a PDF and click in the form to run the next set of data.
That is very time consuming when i have over 500 reports to make. So is there a way to make a function, a VBA or macro to run through all pages in the form and save each report as a PDF?
My form is named NorwF, the query is NorwQ and the report is NorwRap
I hope that makes sense and that there is a faster way to make this projekt run smoothly.
If you place a button on the form, this code behind should work:
Dim rst As DAO.Recordset
Dim strReport As String
Dim strReportFile As String
strReport = "NorwRap"
'Set rst = Me.RecordsetClone
Set rst = CurrentDB.OpenRecordSet("NorwQ") 'use the query
Do While rst.EOF = False
strReportFile = rst!ID & ".pdf" ' give report name based on field name
DoCmd.OpenReport strReport,acViewPreview , , "id = " & rst!ID
DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strOutFile
DoCmd.Close acReport, strReport
rst.MoveNext
Loop

Setting focus to a subform gives the error 2449 (there is an invalid method in an expression) - VBA - Access 2013

-----------UPDATE---------
I figured out how to get the focus on my subform. If it is a subform then :
ParentFormName = frm.Parent.Name
SubFormName = frm.Name
Forms(ParentFormName).Form(SubFormName).SetFocus
This seems to work (although Andre451's post is more effective). But this resulted in the underlying problem of my subform causing the suspended state. My subform is relatively big (194 records) and when doing a MoveFirst/Last on the recordset of larger records I get the error 3021 (no current record). This probably has something to do with the entire recordset of the subform isn't loaded on the load event of the main form. Smaller recordsets can be looped faster so won't generate the error (this is just an assumption though).
The new question is: How can I avoid the 3021 error and let the subform records load entirely so I can move through them with either:
DoCmd.RunCommand acCmdRecordsGoToLast
OR
frm.RecordsetClone.MoveLast
----------------------------------------------------------------------------------
A while a go we migrated our Access data to a SQL server with still using the Access as the frontend. We did this by linking the SQL Server tables with Access and this all works (almost) fine. Recently however we encountered a problem with Access queries getting in suspended state on the SQL server (mainly happens on queries in combo boxes).
After some research we figured out that this could be solved by looping through the query records on the load of the form (so by doing a simple acCmdRecordsGoToLast and acCmdRecordsGoToFirst) and this seems to work.
So to counter this suspended state we added to each forms load an init that runs through the records of the recordsource and rowsource (the main forms recordsource, the comboxes rowsource etc.).
So on a forms load:
modFunctions.InitForm(Me)
This init is in a module and looks like this:
Dim InitFormResult As Boolean
InitFormResult = InitFormRecordSource(frm) ' Initialize the recordsource of the form
InitFormComboBoxes frm ' Initialize all the comboboxes on the form
InitFormCommandButton frm ' Initialize all the buttons on the form
The first init of the forms RecordSource (where the error happens) looks like this:
Public Function InitFormRecordSource(frm As Form) As Boolean
If frm.RecordSource <> "" Then ' if recordsource found
On Error GoTo ErrHandler
frm.SetFocus
DoCmd.RunCommand acCmdRecordsGoToLast
DoCmd.RunCommand acCmdRecordsGoToFirst
GoTo EndSuccess
ErrHandler:
MsgBox (Err.Number)
Debug.Print "ERROR during InitFormRecordSource in form : " & frm.Name
InitFormRecordSource = False
Exit Function
EndSuccess:
InitFormRecordSource = True
Else
InitFormRecordSource = True
End If
End Function
To run through the forms recordset with First and Last it needs to have the focus. This all works great for one form but it doesn't for Subforms in a main form. In subforms we want to counter the suspended state as well by also running through the subforms records, though when using the above code we keep getting the error 2449.
After some research I encountered this post: Module Function frm.setfocus runtime 2449 error which is more or less the same problem. So I figured out that a subform needs to be given it's focus in a different way, yet all my efforts to do this won't work.
What I tried so far:
If frm.RecordSource <> "" Then ' if recordsource found
On Error GoTo ErrHandler
Dim HasParent As Boolean
Dim sParentForm
HasParent = TypeName(frm.Parent.Name) = "String"
If HasParent = True Then 'The form is a subform
'sParentForm = frm.Parent
'sParentForm.frm.SetFocus
'frm.Parent.frm.SetFocus
'sParentForm!frm.SetFocus
Else
frm.SetFocus
End If
DoCmd.RunCommand acCmdRecordsGoToLast
DoCmd.RunCommand acCmdRecordsGoToFirst
GoTo EndSuccess
The tried fixes are all commented out in the above code. The HasParent does work, with this I can check if the form is a subform (it has a parent) or not and setting the focus differently.
Though I can't seem to get the focus on the subform and I don't quite know how to get it fixed!
I have had a similar problem, queries on Sql Server 2008 R2 going into a "ASYNC_NETWORK_IO" wait state, until the last record was loaded.
My solution: don't use the form itself to go to the last record, but its .RecordsetClone:
frm.RecordsetClone.MoveLast