Running VBA code on an event in an Access web database - ms-access

I took a already set up database from the Office.com templates (File --> New --> Office.com Templates), since this template already gave me 80% of my requirements.
I then changed the last 20% of this template where I came into the following: It seems as this template is a web database. I am not sure about the differences of a web database and a normal database in Access at all but I did find out, they do not allow me to run VBA code. At least not when I am trying to call it by an event.
Is there any way to achieve this anyways? Or are there possibly any other ways, such as converting the web database into a normal one perhaps?

Actaully, you can call VBA code from the forms event code. However, it requires a bit of a kluge.
However, you don't need to do this in your case.
Do note that you can create client objects and use client VBA code in your web application.
This means you can add/mix VBA forms into that application (these client objects of course cannot run in a web brwoser, but they can be used the client).
Suggestion #1:
to hide record navigation buttons.
Open the web form in layout mode. Move a object (dirty the design).
Whack ctrl-g for VBA command prompt. Type in this:
forms(0).NavigationButtons = False
Now, save the form. The record navigation setting will be saved with the form.
Suggestion #2:
Open the web form with VBA. Use this code:
DoCmd.OpenForm "AssetsDetails"
Forms("AssetsDetails").NavigationButtons = False
Suggestion #3:
Call VBA code from the forms load event. This is a bit of kluge, but does work:
In the forms open event, place this macro code:
OpenForm (frmRunVBA,,,Dialog)
Note that in above WEB MACRO editor the VBA form above called frmRunVBA does NOT appear in the drop down choice list - but you CAN TYPE/force in the name – this is legal.
Now create a VBA form called frmRunVBA). In the forms load event, palce this code:
Private Sub Form_Load()
Forms("AssetDetails").NavigationButtons = False
DoCmd.Close acForm, Me.Name
End Sub
So you can call VBA code, but you really don't need to if you use tip#1.

Related

How can I refresh my external data source (link to sharepoint) in Access vba?

Really struggling to find a solution to this.
I have an access form, which has a subform.
The subform displays, via a query, records from an external data source (a sharepoint folder). I need the external link to refresh automatically, ideally on load.
Private Sub Form_Load()
TableDefs![My External Table Name].RefreshLink
End Sub
didn't work neither did it without TableDefs!
I'm piecing things together with limited experience, this is my first dabble with access. I had the connection refresh off a macro in excel with
Sub Refresh_click()
Range("MyExternalTableName[[#Headers],[ID]]").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End Sub
and that was fine, now in access I'm lost. I need this subform to be updated before using the rest of the form but could refresh off a button if needs be. I already use a refresh button as part of the form and this doesn't affect the external link unfortunately.

How do I open Class Objects dynamically in the VBA editor without opening the editor?

Edit: See my comment below for a partial solution.
Edit 2: I found an adequate solution for closing the VBA editor but just want to see if anyone knows how to make it fully invisible the whole time. What I have found out works for my needs but I will leave this thread open for anyone who wants to elaborate on another method or expand on mine.
Original Post: I have a function that creates tables, queries, and forms. For the forms, it copies a template form and calls a function that replaces the forms VBA code dynamically. The below function I created works great, however, if I do not have the class object form open in the editor, I get the Run-Time error '2516': "Microsoft Access cannot find the module 'SPCInputFormVBA.', where SPCInputFormVBA is a variable for the class objects name. To further elaborate on the behavior, if I have the editor closed, as long as the module or class object is open in the editor, it will still work. I would like to be able to activate the corresponding Form_xyz object in the editor without the editor opening so that I can use this function to do a bunch of stuff to the to it. I am using a template form because there are a lot of things nested in it and has a lot going on. The form I can change the record source and various other things in it just fine, but the VBA portion is elusive to me so far. I thought opening the object would be easy but I am having a lot of trouble and cannot find a way to describe my issue that leads to me finding a solution.
Public Function InputFormVBA(SPCInputFormVBA)
DoCmd.OpenModule (SPCInputFormVBA)
Dim i As Integer
With Application.Modules(SPCInputFormVBA)
For i = 1 To .CountOfLines
If InStr(.Lines(i, 1), "TempTable") > 0 Then
' .ReplaceLine i, " If DCount( ""serial"", """ & tblName & """, _"
End If
'If Instr(.Lines(i, 1), "
Next i
End With
End Function
The closest I have come is trying various things with this function in order to help me understand how Access will react to different potential solutions I find online:
Sub PrintOpenModuleNames()
Dim i As Integer
Dim modOpenModules As Modules
Set modOpenModules = Application.Modules
For i = 0 To modOpenModules.Count - 1
Debug.Print modOpenModules(i).Name
'DoCmd.OpenModule (modOpenModules(i).Name)
Next
End Sub
The 'DoCmd.OpenModule (modOpenModules(i).Name) is commented out in this example and will open things, but it only sees things that are open already which doesn't help me. I do understand that there are different types of Modules, but I am not sure how to distinguish and the documentation online explains a general difference but doesn't reveal any way to reach out to the Class Object unless it is open in the editor already. Hoping someone can help or even correct my terminology if it is off and steer me to an existing solution elsewhere on the site.
You can use the VBE object model to access your form's code module without opening it in the VB Editor. And with that approach, the VB Editor window does not need to be visible or open. If the VB Editor window is not open, accessing the module this way will not open it. So I think that satisfies the main objective of your question.
It's possible to use VBE with late binding but, since you're not familiar with it, you'll likely want to use early binding instead. If so, add Microsoft Visual Basic for Applications Extensibility to your project's references.
Here is a very minimal procedure which only shows you how to reference a code module by name and print 2 of the module's properties.
Public Sub InputFormVBA(ByVal SPCInputFormVBA As String)
Dim objModule As CodeModule
Set objModule = Application.VBE.VBProjects(1).VBComponents(SPCInputFormVBA).CodeModule
With objModule
Debug.Print .CountOfDeclarationLines
Debug.Print .CountOfLines
End With
End Sub
If your database contains only one VBA project, VBProjects(1) will reference it. But if the db contains more than one VBA project, you may need to give VBProjects() a different number. I presume you'll figure that out pretty darn quick. :-)
A CodeModule object has methods you should find useful, including:
DeleteLines; Find; InsertLines; and ReplaceLine. However I don't really know what you want to do with the module's code, so will just leave it at that.

Call an embedded macro from VBA in access

I have a macro assigned to the onClick event of a button in a form. How can I call this macro programmatically?
I tried
btnName_Click
But this does not work since there is no function called btnName_Click() ... obviously :)
I can access the onClick Member via Me.btnNewRecord.OnClick but don't see a way to run the macro.
After extensive searching, I do not believe it is possible to reference the embedded macro, and run it. You can view the XML of the macro, but I know of no way of running it or even accessing it beyond it's XML stored as a string. A possible work around would be to convert all macros to VBA. To do this:
Open the form in design view.
Click Convert Form's Macros to Visual Basic
now you should be able to call the button's code with btnName_Click as you showed in your question. Obviously if you did this, you would sacrifice the advantage of using macros (i.e. limited functionality without the user needing to trust your database).
Original Answer, which doesn't apply to Embedded Macros:
Use DoCmd.RunMacro
Example:
Docmd.RunMacro(macroname)
where macroname is a string representing the name of the macro.

Creating access CommandBar with timer event using VBA

I have Office 2003.
I would like to create Commandbar with timer in MS Access using VBA.
What I want is on ever half a second command bar button gets the name of active forms and lists name of forms.
How can I do that?
You can create a CommandBar using a Macro object in Excel 2003. However, needing to update it every 1/2 second is complete overkill. Forms won't get added or dropped very frequently, so you should have a static list for both performance and maintenance reasons.
The best way is to actually make a custom menu (an AddMenu line in the Autoexec macro) that calls a different macro with all the menu entries in it. Actual command bars require a good amount of VB to make them work properly.

Show code from from thru code (in ms access)

making an Access database (2003) that is mainly used by other programmers and tech management as a tool for data validation. I want to put an button on parts of the form that runs code, to show the code that is being run. So the programmers can edit it on the spot. This is for a data validation project with frequent changing of code. Lets assume the code is in a module so the form doesn't need to be saved or in edit mode. Just push the button and up pops the code in code view.
You may be limited to only code in modules. But in a button on the form, you could set the click event to:
DoCmd.OpenModule "module_name", "the_name_of_some_function"
Would it work for you?
Private Sub Command0_Click()
While True
Debug.Print "tt"
If pressed And debugmode Then
Stop
pressed = False
End If
DoEvents
Wend
End Sub
Private Sub Command1_Click()
pressed = True
' You can just put here Stop
End Sub
Edit: I mean use DoEvents and Stop
Viewing the code is not so hard, and really neither is editing it. But I should warn you off a few pitfalls. First, the approach you are discussing amounts to self modifying code which will trip a lot of Antivirus software. Secondly corruption is a constant danger with Access and self modifying code only exacerbates this problem. Any solution you come up with should minimize edits and you should also have a very robust backup and restoration plan. Thirdly self modifying code will prevent you from compiling the database (using mdes) or locking the project. This can lead to some very hard to control and maintain source code.
I would suggest instead that you consider using Pure SQL to do your data modification. You can then store and modify the SQL in a table and then just use CurrentDB.Execute to run it.