Run Sub-procedure with Procedure name as a string - ms-access

I'm wanting to run a procedure from a form that was open via openargs
This is how I am opening the form.
DoCmd.OpenForm "Loading_Form", acNormal, , , , acWindowNormal, DynaProgBarMax & "|" & DynaLableCaption & "|" & ProcCall & "|"
This is what is ran when the form opens.
Private Sub Form_Open(Cancel As Integer)
Dim OpenArgsAry As Variant
OpenArgsAry = Split(Me.OpenArgs, "|")
Me.DynaProgBar.Max = OpenArgsAry(0)
Me.DynaLable.Caption = OpenArgsAry(1)
Run OpenArgsAry(2)
End Sub
I am having an issue with Run OpenArgsAry(2). I'm getting an error stating that the Procedure can not be found. OpenArgsAry(2) is containing a string "Forms(""Stuff"").PrintAllStuff()". When I Replace Run OpenArgsAry(2) with Run Forms("Stuff").PrintAllStuff() it works fine.
Run is suppose to be able to run a string as the procedure name is it not?

Run requires a procedure (sub or function in a global module, not a form or class module). PrintAllStuff is a method on a form, thus can't be executed through Run.
However, you can execute it through CallByName:
CallByName Forms("Stuff"), OpenArgsAry(2), vbMethod
Where OpenArgsAry(2) only contains PrintAllStuff. You can use Me instead if you want to execute the method on the current form.

Related

Why is is my MS Access control not accepting my "on not in list" event code

I've been using the Access "On Not In List" event for a long time. It allows you to limit the items in your combo box to a particular list, but allows the user to add an item to combo's record source on the fly if they want to enter something that isn't already there. You set the "Limit To List" property of the combo box to Yes, but then you put some code behind the "On Not In List" event. Intermittently, I get a situation in Access 2016 where this doesn't seem to work. I get the standard "The item is not in the list." error when trying to enter a new item, without my code-behind logic being seen and called. What's up?
After banging my head against the wall for a long time, I believe this is bug in Access 2016 and I think I stumbled on a fix. I set the form's RecordSetType property to Snapshot, closed and saved form, reopened the form in design view, set the RecordSetType property back to Dynaset. This seems to have cleared up the problem. I have no idea why.
But since I'm here. . . some additional details: In the code-behind for each control I use code like this:
Private Sub VendorID_NotInList(NewData As String, Response As Integer)
Response = RU_NotInList("Lookup Vendor", "Description", NewData, gc_strMsgCap)
End Sub
This type of subroutine gets created automatically behind the "On Not In List" event, when you click on the 'code builder' option. I have mine call a utility function that I wrote a long time ago. ("RU" refers a code library.)
The function returns an intrinsic Access integer constant that gets passed straight back to Access to handle.
The inside of that routine looks like this:
Function RU_NotInList(TableName As String, FieldName As String, newdata As String, Optional pstrTile As String) As Integer
Dim rs As DAO.Recordset, db As DAO.Database, n1 As Integer
RU_NotInList = DATA_ERRCONTINUE
On Error GoTo RU_NotInList_Error
If Len(Trim(newdata)) = 0 Then Exit Function
n1 = MsgBox(newdata & " is not in the list. Do you wish to add it?", MB_ICONQUESTION + MB_YESNO, pstrTile)
If n1 = IDNO Then Exit Function
Dim strSQL As String
strSQL = "INSERT INTO [" & TableName & "] ([" & FieldName & "]) VALUES (""" & newdata & """)"
WarningsHour True 'Turns hourglass cursor on, warning messages off.
DoCmd.RunSQL strSQL
WarningsHour False 'Undoes the above.
RU_NotInList = DATA_ERRADDED
Exit Function
RU_NotInList_Error:
RUError "RU_NotInList", Err 'generic error-handling routine in the RU library
Exit Function
End Function
All the all-caps items in the code above are Access intrinsic constants.

Access Macro Converted to VBA then Editted only Executing Access Defined Code

I have set up a basic macro where I export the contents of a table to excel. Macro works great, but now I want to create a check to see if the file name that I am saving to already exists, and if so, deletes that file so that I do not have the user deal with the prompt box asking if they would like to overwrite the file.
I converted the macro to VBA so that I could add in the desired dir(filename) and kill(filename) code. Once completed I was able to successfully run the code in the VBA editor, however, when I tried running the code based on the "on close" event of a form I have in access, it will only run the code as viewed in the access macro structure, as if I never added any additional lines of code in the VBA editor. Is there something that I was supposed to do to convert back from VBA to Access once I completed my edits?
Please see below for code that I would like to execute:
Function ExportLot()
On Error GoTo ExportLot_Err
Dim filename As String
filename = "\\server1\Trial
Database for QS Reports\Lot Log Report.xlsx"
DeleteFile (filename)
DoCmd.OutputTo acOutputQuery, "LLUnion", "ExcelWorkbook(*.xlsx)", filename, False, "", , acExportQualityPrint
ExportLot_Exit:
Exit Function
ExportLot_Err:
MsgBox Error$
Resume ExportLot_Exit
End Function
Function FileExists(ByVal FileToTest As String) As Boolean
FileExists = (Dir(FileToTest) <> "")
End Function
Sub DeleteFile(ByVal FileToDelete As String)
If FileExists(FileToDelete) Then 'See above
' First remove readonly attribute, if set
SetAttr FileToDelete, vbNormal
' Then delete the file
Kill FileToDelete
End If
End Sub
Original converted code (what currently runs when I call ExportLot from the form):
Function ExportLot()
On Error GoTo ExportLot_Err
Dim filename As String
DoCmd.OutputTo acOutputQuery, "LLUnion", "ExcelWorkbook(*.xlsx)","\\server1\Trial Database for QS Reports\Lot Log Report.xlsx", False,"", , acExportQualityPrint
ExportLot_Exit:
Exit Function
ExportLot_Err:
MsgBox Error$
Resume ExportLot_Exit
End Function
I managed to figure out either the solution to or a work around for this problem. I created a new macro (ExecuteCloseCode), and used the RunCode Event to call my function (ExportLot()) as defined in the question.
Then I used the on close event to call the "ExecuteCloseCode".
I am not sure why this method worked and my previously proposed efforts did not...

Use vba to refresh & renew table links

I have designed a system that is used to track customer activity and log calls to a department. The front end and back end database are written in access. This system is due to go to the USA division of the company i work for.
The front end needs to automatically refresh the tables and if the backend database has moved (which it will when i send it to the US) the code will then look at a function to read the location of the new database. Sample of the read text file function code shown below:
Function ReadDbPassword()
'--
' Filetostring(FILEInput$ as variant) ' to make this a callable function
Dim FILEInput As Variant
'--
On Error GoTo FileToString_Error
FILEInput = "C:\Users\Public\databaseUser\PassCon"
Passmyfile = FreeFile
Open FILEInput For Input As Passmyfile
Passthedata4 = Input(LOF(Passmyfile), Passmyfile)
Close Passmyfile
On Error GoTo 0
Exit Function
FileToString_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
End Function
The text file contains a path like the one below:
P:\Projects\Database.accdb
I have found code that uses a similar idea to what i want and i have been looking at the code on the link below, however i do not fully understand how this code works in order to alter it to what I need to use the read text file.
http:/ /access.mvps.org/access/tables/tbl0009.htm
-------EDIT --------
I have tried to edit the following section to use the read text function
Function fGetMDBName(strIn As String) As String
'Calls GetOpenFileName dialog
Dim strFilter As String
strFilter = ahtAddFilterItem(strFilter, _
"Access Database(*.mdb;*.mda;*.mde;*.mdw) ", _
"*.mdb; *.mda; *.mde; *.mdw")
strFilter = ahtAddFilterItem(strFilter, _
"All Files (*.*)", _
"*.*")
fGetMDBName = ahtCommonFileOpenSave(Filter:=strFilter, _
OpenFile:=True, _
DialogTitle:=strIn, _
Flags:=ahtOFN_HIDEREADONLY)
End Function
By replacing all the code with
fGetMDBName = Passmyfile
You are mixing apples and oranges in what you are trying to do. Here are my suggestions:
Make sure your module has 'Option Explicit' then compile all your code. I see variables referenced but have no idea what TYPE they are.
Change your Function 'ReadDbPassword()' to return a string variable, then set it to return Passthedata4.
The second Function you listed (fGetMDBName) is opening a File Dialog box to allow you to select a file name. You do not need that since you already will have the file path/name from your first Function.
Then adapt the code you found that does the relink to use the path/name from your subroutine.

Function to determine and pass value in a form control

I am trying to create a function that will determine the value held in an active form control and then pass the value for a form criteria as follows:
Public Function fc_id() As Long
fc_id = (Screen.ActiveControl.Caption)
End Function
Public Sub sShowForm()
DoCmd.OpenForm "F_Details", , , "c_id = " & fc_id
End Sub
I then call the function from the form control being clicked:
sShowForm
The details form opens but it's black, and there is not error message.
If I use a numeric value, what would be the equivalence of the
(Screen.ActiveControl.Caption)
Temporarily change your procedure to find out what you're getting from Screen.ActiveControl:
Public Sub sShowForm()
'DoCmd.OpenForm "F_Details", , , "c_id = " & fc_id
MsgBox Screen.ActiveControl.Name
End Sub
You may not be getting what you expect. For example, if you're using a command button to run sShowForm(), the command button is the ActiveControl. Whether or not that is the situation, check to see what you're actually getting.
If the present form (the form where your VBA code is running) includes a text box bound to a c_id field in the form's record source, you can eliminate the function and reference that text box directly in the OpenForm option. For example, if c_id is a numeric field and the text box is named txtc_id ...
DoCmd.OpenForm "F_Details", , , "c_id = " & Me.txtc_id
If c_id is text type, add quotes around the text box value ...
DoCmd.OpenForm "F_Details", , , "c_id = '" & Me.txtc_id & "'"

Does it degrade performance to use subforms in MS Access?

I am considering the use of a tab control on a parent form for which I would like to have around 20 tabs. Each tab I am considering the use of one or two separate sub forms. Each sub form will have varied complexity in coded logic. By taking this approach will I severally reduce the performance of my application? I am currently using this in MS Access 2003. I will expect an average of 15 users at any given time on the various forms.
Thoughts?
Yes, performance will be degraded slightly for each subform. One or three isn't too bad but twenty is definitely going to cause you performance issues.
Once you have the subform working to your satisfaction either save the Record Source as a query and give it a name or save the query SQL string. Then either paste the query name or the query SQL string in the VBA code in the tab control change event.
Private Sub TabCtl_Change()
On Error GoTo TabCtl_Change_Error
Select Case Me.TabCtl.Value
Case Me.pagPartsConsumed.PageIndex
If Me.PartsConsumedsbf.Form.RecordSource <> "Equipment - Parts Consumed sbf" Then _
Me.PartsConsumedsbf.Form.RecordSource = "Equipment - Parts Consumed sbf"
....
Now just to ensure that I don't accidentally leave some subform recordsources filled in slowing down the app on startup I check to see if the file the code is running is an MDB (instead of an MDE. The function is below) then display a message telling me I have to remove the recordsource.
If Not tt_IsThisAnMDE Then
If Me.PartsConsumedsbf.Form.RecordSource <> "" Then _
MsgBox "Record source of Equipment - Parts Consumed sbf not empty"
...
End If
Public Function tt_IsThisAnMDE()
On Error GoTo tagError
Dim dbs As Database
Set dbs = CurrentDb
Dim strMDE As String
On Error Resume Next
strMDE = dbs.Properties("MDE")
If Err = 0 And strMDE = "T" Then
' This is an MDE database.
tt_IsThisAnMDE = True
Else
tt_IsThisAnMDE = False
End If
Exit Function
tagError:
Call LogError(Application.CurrentObjectName, "")
Exit Function
End Function
Also in the form unload event I clear the Recourd Source as well.
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Form_Unload_Error
Me.PartsConsumedsbf.Form.RecordSource = ""
....
BTW I almost always would put each subform on a seperate tab. Also that many tab entries gets visusally unwieldy. When I had a similar question my fellow Access MVPs suggested using a listbox along the left hand side to control which subform is viewable.
Also each combo box and list box will also slightly degrade the performance. So if you have those on a subform then consider similar logic.
In addition to adding recordsets at runtime, I would generally only use one or two tabs and a number of controls to load various subforms into a subform control.
The text for the On Click event of the control might be:
=WhichPage([Form],"lblLocations")
Where WhichPage is a function with the following lines, amongst others:
Function WhichPage(frm, Optional LabelName = "")
<..>
Select Case LabelName
Case "lblLocations"
frm("sfrmAll").SourceObject = "sfrmLocations"
<...>
If necessary, the link child and link master fields can be changed at runtime. The link master field is best set to the name of a control, rather than a field, to avoid errors.
Me.sfrmAll.LinkChildFields = "LocationKey"
Me.sfrmAll.LinkMasterFields = "txtLocationKey"
To expand on Remou's answer...here is a sub I wrote that dynamically loads a form into a subform control. You pass in the name of the form in the call and it will load it into the subform of the Main form. The arguments map to the arguments of Docmd.OpenForm method of Access. If the main form that is hosting the subform control is not open...it just does a regular open of the form. Otherwise it loads it into the subform control. If a where clause was passed in it is used to filter the subform.
Public Sub MyOpenForm(FormName As String, _
Optional View As AcFormView = acNormal, _
Optional FilterName As String = vbNullString, _
Optional WhereCondition As String = vbNullString, _
Optional DataMode As AcFormOpenDataMode, _
Optional WindowMode As AcWindowMode, _
Optional OpenArgs As String)
On Error GoTo PROC_ERR
Dim frm As Form
Dim strNewForm As String
Dim strCurrentForm As String
Dim strNewTable As String
Dim fDoNotFilter As Boolean
Dim strActionText As String
Dim strID As String
If Not IsLoaded("frmMain") Then
DoCmd.OpenForm FormName:=FormName, View:=View, FilterName:=FilterName, WhereCondition:=WhereCondition, DataMode:=DataMode, WindowMode:=WindowMode, OpenArgs:=OpenArgs
Else
strCurrentForm = Forms![frmMain]![sfrMyForm].SourceObject
If strCurrentForm <> FormName Then
Forms![frmMain]![sfrMyForm].SourceObject = vbNullString
Forms![frmMain]![sfrMyForm].SourceObject = FormName
End If
If WhereCondition <> vbNullString Then
Forms![frmMain]![sfrMyForm].Form.Filter = WhereCondition
Forms![frmMain]![sfrMyForm].Form.FilterOn = True
End If
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox Err.Description
Resume PROC_EXIT
End Sub