Is there a way to convert my old data macros from table to vba after a database split? - vba

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.

Related

Microsoft Access attempting to find table in macro as field

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

MS Access: Add attachment to record from Form field using VBA

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.

How do I populate the destination of a report export based on the value of a text box

I currently have the following VBA code running:
DoCmd.OutputTo acReport, "r_GRV_DETAIL_EXPORT", "MS-DOSText(*.txt)", "C:\Application\TSClient\Bin1\Scans\123.txt", False, ""
However, I will have different users using the application and they each have a specific location where they would need to export the report to (bin2, bin3 etc). I am going to have them select the user name and based on that the export location will be populated in a text book.
Question: How do I go about populating the above code with the location as defined in the text box (call it: txt_MAIN_SCAN_LOCATION).
Any help will be greatly appreciated.
Please try the next approach:
Dim strSpecif as String
strSpecif = Forms![frm_MAIN_MENU]![txt_MAIN_SCAN_LOCATION].Value
Then, use this new variable in your code:
DoCmd.OutputTo acReport, "r_GRV_DETAIL_EXPORT", "MS-DOSText(*.txt)", strSpecif , False, ""
Using of the Value property will not make necessary the previous SetFocus on the text box, like in case of Text property.

Set MS-Access Image Control Source from Attached Image Using VBA

I'm adding a photo to form to display for each user with other info of that user. I created a tabbed form, on page one I select a user and press a button that runs the following code:
Private Sub Command106_Click()
Dim qry_rs As DAO.Recordset
Dim qry_db As Database
Set qry_db = CurrentDb
SQLString = "SELECT [Clients Info].* FROM [Clients Info] WHERE ((([Clients Info].Info_Client)='" & [Forms]![Form-1]![Combo13_PageOne_Name] & "'));"
Set qry_rs = qry_db.OpenRecordset(SQLString)
Forms![Form-1].[Info_Member].Value = qry_rs![Info_Member]
Forms![Form-1].[Info_Tower].Value = qry_rs![Info_ID]
End Sub
I tried using the same way that I set the values of that textboxes in setting an image control value
Forms![Form-1].[Info_Picture].Value = qry_rs![Info_UserIDPhoto]
But it's not working, can anyone help?
I receive this message:
Run time error 2465:
Microsoft Office Access can't find the field "|" referred to in your expression
I'm using Office 2007, on Windows 7
To my knowledge, there is no way to refer to the ControlSource property through vba. However, using the property sheet in Design View works PERFECTLY. Use the value of your combo box as the criteria in a DLookup expression.
Enter this as the ControlSource of your image control:
=DLookUp("[YourAttachmentField]","[YourTable]","[YourNameField] = '" & [YourComboBox]. [Column] (x) & "'")
(Make sure you're referencing the correct column; I'm just using x as an example.)
This won't work if you alter the attachment's name or location AFTER it's been attached, so if any changes are made you must reattach.
VBA allows you to set the Picture property:
Forms![Form-1].[Info_Picture].Picture=qry_rs![Info_UserIDPhoto]
The Picture property value must be a file name--or complete path and file name if not found in the default folder set in Access options.
When set from VBA, the value may be a statement or variable whose value is a (path and) file name. In the case above, the field [Info_UserIDPhoto] would contain the complete path and file name of each user's ID photo.
(Unlike the ControlSource property, you cannot enter a variable or field name for this property directly in the design view property page.)
You can reference the control source property by doing the following:
Dim c As Control
Set c = Forms("FormName").Form.Controls("ControlName")
c.Properties("ControlSource") = "Your control source name here"

MS Access: How to get value of textbox on button click?

This is a complete newbie question, but I couldn't find any solution online (at least nothing to my understanding). I want to make a form with a textbox and a button and on click, the button should take the value written in the textbox, find the row with that ID in a table and change an attribute to unavailable. Normally I would do this with a simple SQL statement:
UPDATE table SET available='NO' WHERE id = *textbox.value*;
What should I write instead of textbox.value so that it works?
You can refer to an open form:
UPDATE table SET available='NO' WHERE id = Forms!MyForm!MyTextbox
Assuming that the form is the main form and not a subform. You can also run SQL in VBA. Make sure you do not update a record via a form and via a query or code at the same time.
Re Comments
In this case, the problem is corruption, something I often forget to watch out for. You will find a good article here : http://www.granite.ab.ca/access/corruptmdbs.htm, including Decompile.
Getting the value from the text box is even easier if:
the query is executed from VBA
the VBA is in the same form as the text box
If I understood the question right, this is the case here.
Then you can just do this:
Dim SQL As String
'case 1: when the id is a string value
SQL = "UPDATE table SET available='NO' WHERE id = '" & Me.NameOfTheTextBox & "';"
'case 2: when the id is a numeric value
SQL = "UPDATE table SET available='NO' WHERE id = " & Me.NameOfTheTextBox & ";"
CurrentDB.Execute SQL