I am creating a database for my school which contains data for an event happening later on in the year. They need a query that displays the events and how many people are going. At the same time they want me to display an error if the venue is overbooked.
I attempted to do this via a macro. The macro would open the attendence_rpt report, then run the macro to see whether the venue is overbooked, like so:
'------------------------------------------------------------
' Macro1
'
'------------------------------------------------------------
Function Macro1()
On Error GoTo Macro1_Err
DoCmd.OpenReport "attendence_rpt", acViewReport, "", "", acNormal
If (Reports!attendence_rpt!CountOfAttendeeID > Reports!attendence_rpt!venue_tbl!Capacity) Then
Beep
MsgBox "test", vbOKOnly, "test"
End If
Macro1_Exit:
Exit Function
Macro1_Err:
MsgBox Error$
Resume Macro1_Exit
End Function
When I run the macro however, it comes up with this error:
Microsoft Access can't find the field 'venue_tbl' referred to in your expression
This means that the macro is trying to find the linked table 'venue_tbl' as a field.
Is there any way to fix this? Or is there an easier method to achieve the same goal?
Below are some photos of the database in Access:
Add a new field to the report that contains venue_tbl!Capacity and make it not visible (unless you want to show the new field to the user). Then change the line in your macro:
If (Reports!attendence_rpt!CountOfAttendeeID > Reports!attendence_rpt!venue_tbl!Capacity) Then
to:
If Reports!attendence_rpt!CountOfAttendeeID > Reports!attendence_rpt!NameOfNewField Then
where NameOfNewField is the name of the field you added.
You must not reference the table in the report.
So this is WRONG:
Reports!attendence_rpt!venue_tbl!Capacity
And this is CORRECT:
Reports!attendence_rpt!Capacity
Related
I recently split my ms access database into a front, and back end. I didn't know that the data macros that were on the table wouldn't be able to work anymore. I was wondering if there is any way to convert these to vba.
if IsNull([prmQuoteID])
RaiseError
Error Number 10
Error Description Please specify quote details.
end if
RunDataMacro
Macro Name Quotes.GetNextQuoteNumber
SetLocalVar
Name varNextQuoteNumber
Expression = [ReturnVars]![retNextQuoteNumber]
RunDataMacro
Macro Name Quotes.UpdateQuoteTotal
parameters
prmQuoteId = [prmQuoteId]
if [varNextQuoteNumber]=0
RaiseError
Error Number 20
Error Description Unable to retrieve next quote number!
stopmacro
end if
Look Up A Record In SELECT Quotes.ID, Quotes.StatusID, Quotes.QuoteNumber, Quotes.Submitted FROM Quotes;
Where Condition = [ID]=[prmQuoteID]
if [StatusID]>=10
RaiseError
Error 30
Error Description Quote has already been marked submitted.
End If
Edit Record
SetField
Name StatusId
Value = 10
SetField
Name QuoteNumber
Value = [varNextQuoteNumber]
SetField
Name Submitted
Value = Date()
Example of Data Macro
Data Macro Photo of above code
It connects to an form on click embedded macro in the front end database
On click embedded macro
OnError
go to Macro Name
ErrorHandler
SetTempVar
Name YesNoMessage
Expression = "Quote cannot be changed once it is submitted."
SetTempVar
Name YesNoMessage2
Expression = ""Do you want to continue?""
OpenForm
Form Name YesNoAlert
View Form
Window Mode Dialog
If [TempVars]![YesNoResponse]=False
stopmacro
End If
RunMenuCommand
command saveRecord
RunDataMacro
Macro Name Quotes.Submit
Parameters
prmQuoteId = [txtID]
RefreshRecord
RunMacro
Macro Name SetFromState
SetTempVar
Name AlertMessage
Expression "Quote #" & [txtQuoteNumber] & " is now submitted for approval."
OpenForm
Form Name Okalert
View Form
Window Mode Dialog
Submacro: ErrorHandler
RunMacro
Macro Name Utilities.ErrorAlert
End Submacro
I was wondering what the best way to convert these would be and still have them work for someone with limited VBA knowledge.
I have a bounded form with property 'Data Entry = Yes' which has a multiple textboxes, and one field of type Attachment.
I have a "Save" button that prompts confirmation to save the record.
On the form I placed a checkbox labelled 'Includes training'.
If that checkbox is checked, I want to add to the same table, an additional record (other than the one added through the bounded form) with exactly the same information as the record inserted in the bounded form, except for one text field which will be different and defined in the VBA code.
I tried a CurrentDB.Execute SQL query but it does not work with the Attachment field type.
The solution proposed in the documentation doesn't apply to this case since I want to take the Attachment contained in the bounded Attachment field on the form and not from a path on disk.
I think something like the following could work, but when I test it, it saves the record from the bounded form, but not the additional one I want to add with the VBA code, AND it ends with the error:
424 Object Required
The VBA code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctl As Control
On Error GoTo Err_BeforeUpdate
If Me.Dirty Then
If MsgBox("Do you want to save the new record?", vbYesNo + vbQuestion, _
"Save Record") = vbNo Then
Me.Undo
MsgBox "Changes discarded."
Else
If Me.checkbox.Value = True Then
Set rsTable = db.OpenRecordset("Table")
With rsTable
.AddNew
!TextField1 = Me.TextField1.Value
!TextField2 = "My Own Text Field"
!AttachmentField = Me.AttachmentField.Value
.Update
.Bookmark = .LastModified
End With
MsgBox "Record was correctly saved."
End If
End If
End If
End Sub
Thanks a lot for any help.
EDIT:
So clearly this is not the right way to save a new record, but I need this record to be save AT THE SAME TIME (or just after) the one on the bounded form. I DON'T want the user to fill in another bounded form.
The reason for this is because what I am entering are language certification records, and there are certifications that are broader and include two levels.
Hence the checkbox signals "do you want to include the previous level as well?", if it is checked, then the PDF certificate uploaded will be valid both for level 1 and level 2.
All information will be the same except for the level name of the record.
I need these two to be separate records because I can also have them singularly and later I check conditions based on these individual language levels.
To use an attachment field you to use the special methods LoadFromFile to put an attachment in the DB and SaveFromFile to get the attachment back out of the DB. It looks like you attempted to assign a string to an attachment field.
I don't see where db is defined. Did you mean CurrentDb ?
The other problem I see is that you are using bang notation in a With block. Bang notation is a string lookup. If you want to call the item by name you would do so like this:
With CurrentDb.OpenRecordset("Table")
.AddNew
.Fields.Item("TextField1").Value = Me.TextField1.Value
.Fields.Item("TextField2").Value = "My Own Text Field"
.Fields.Item("AttachmentField").LoadFromFile Me.AttachmentField.FileName
.Update
.Bookmark = .LastModified
.Close
End With
The name of those Items must be correct.
I eventually solved this by creating a separate bounded from that pops up when I hit "Save" on the original bounded form if the checkbox is checked. There the user has the choice on whether to upload the same document as the original bounded form, or a different one.
I build a form where users can create a new record and fill the fields. One of those fields is the attachment field. Before they can save the record, I want to check wether they filled in all mandatory fields, the attachment beeing one of those. With all other fields, it works like this:
If IsNull(date_of_useage) Then
MsgBox "Please fill in the date!", vbExclamation
Exit Sub
End If
This does not work für the attachment field, addressing it by its name "attachment". How can I check, if the user did or did not attach a file? IsEmpty didnt work either.
Found a Solution:
If Attachment.Attachmentcount = 0 Then [...]
I've been tasked with creating an Access 2003 application that will act as a focal point for all other databases (6 in total).
These 6 databases will each contain a macro used to build data bound for a table called DispenseExport.
Originally each of these databases had their own version of this table but now we have to have them all write to the one application - which I've affectionately codednamed Omega.
Omega will contain the DispenseExport table that all the others will write to. It will also be the database that calls the macros from the others to write to it.
So - what is the best way to accomplish this?
I already have a sample sub to call a macro from another database (and works) but the problem here is that it opens the database as any normal user would - Omega will sit on a server and needs to bypass this - by possibly using the SHIFT-KEY access method, if best?
How do I do that programmatically in VBA, assuming it's the best way to do so?
Here's what I have so far:
Option Compare Database
Option Explicit
'Sub to call a macro from another database
Public Sub CallMacro()
On Error GoTo ErrHandler:
Debug.Print "Opening Macro"
'/x tells the database to run the macro as soon as it is opened
Call Shell("msaccess.exe C:\path-to-database\database.mdb /x mcrTestCall", 0)
Debug.Print "Completed Macro"
ErrHandler:
If Err.Number <> 0 Then
'Create a message box with the error number and description
MsgBox ("Sorry but error number " & Err.Number & " occurred; " & vbCrLf & Err.Description)
End If
End Sub
UPDATE
Still haven't found the answer but believe that I'm closing in. I need to find out how to emulate HOLDING DOWN SHIFT - any ideas?
Thanks
This will avoid shell call (Access 2007, also Access 2003?):
'
' variables:
' strDbName: database filename
' strMacro: macro name
'
Sub CallMacro()
'
Dim strDbName, strMacro
Dim objApp
'
strDbName = "C:\path-to-database\database.mdb"
strMacro = "mcrTestCall"
'
Set objApp = CreateObject("Access.Application")
'
objApp.OpenCurrentDatabase strDbName
'
' Run Macro:
'
objApp.DoCmd.RunMacro strMacro
'
objApp.Quit
Set objApp = Nothing
'
End Sub
To skip startup form or AutoExec, there is no simple solution, but we have a workaround in http://access.mvps.org/access/api/api0068.htm, by simulating the Shift key, using API.
There is a cleaner way to do this without opening the remote database. Build a query within that database that does what you want, then call it from the other database without ADO, DAO or opening it. Below is a link explaining how:
Remote Queries
Since links go away, the gist of it is this:
1. Build the query you want to call to return what you need in the other database
2. In the remote calling database, open a query without selecting a table (I use Query Design)
3. Right click on the query background and select 'Properties'
4. Replace the Database setting of (Current) with the full path to the database with the query
5. Build a query to call the original query as the datasource
6. Output the data by a "SELECT INTO" or DoCmd.TransferSpreadsheet, etc
I'm having the Run Time Error '2501' on a Form. Does anyone have ideas of why this happens and how to fix it?
The Report is called through this code:
Private Sub Command92_Click()
DoCmd.SetWarnings Off
''# get current reqid and version number
DoCmd.OpenQuery "NewReqVersion", acViewNormal
Dim NewReqID As Integer
Dim NewVerID As Integer
NewReqID = Me.Text58
NewVerID = Me.Version + 1
[Forms]![UWReviewForm].[StatusID] = 99
On Error Resume Next
**DoCmd.Save acForm, "UWReviewForm"**
DoCmd.OpenForm "NewReqVersionForm", acNormal
DoCmd.GoToRecord acDataForm, "NewReqVersionForm", acLast
End Sub
Private Sub StatusID_Change()
Me.LastModifiedTimeStamp = Now()
End Sub
The error 2501 can occur on calling OpenForm if there is a problem with the database. Can you please try following the steps in the link below under the headings "Recovering from corruption" and "Symptom: Cannot open a form or report"?
MS Access - Recovering from corruption
In my case, I had a table linked to an external data source, which was another Access database. I had experimented with splitting that database application into its database component and UI component, and had decided to revert the change. Somewhere along the line, this corrupted to the link between my original Access application and the external Access data source. I could verify this by attempting to open the linked table in my original Access application, and it gave an error.
My solution was to delete the linked table from my Access application and import it once again. After this, my problem resolved. The forms which could not open before were based on queries which included this external data source.