Show code from from thru code (in ms access) - 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.

Related

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.

do you want to save changes to the design of the form

I have developed a Microsoft access 2010 database that uses navigation subforms nested a couple layers deep. When the user clicks on navigation tabs to change the form displayed in the subform (sourceobject), a dialog box appears asking the user:
"Do you want to save changes to the design of the form 'form name'?"
This is obviously unacceptable in a deployed database. Can someone please explain how to get rid of this behavior, so that users can use the navigation tabs without being asked if they want to change design of the form?
I have already programmed logic allowing users to decide whether or not to save data changes. I do not want the user to be prompted about design changes.
The only thing I've been able to get to work is to turn warnings off in the OnEnter event for each navigation button, and turn them back on in each 'sub' form's Open event - e.g.
Private Sub navbtnDetails_Enter()
DoCmd.SetWarnings False
End Sub
and
Private Sub Form_Open(Cancel As Integer)
DoCmd.SetWarnings True
End Sub
This should be the Open event, rather than the Load event, because the Load event occurs after the form's data is loaded.
One benefit of this approach is that it persists changes to any datasheets - column widths, sorting, etc., though that may or may not be a good thing, depending on how you need the UI to work.
This is with Access 2013.
try to set AllowDesignChanges property to "Design View Only"
I know this is an old question, but this solution may help others:
Manually: uncheck "Enable design changes in Datasheet view" in File, Option, Current Database.
Programmatically: CurrentDb.Properties("AllowDatasheetSchema") = false
This is one of the best practices before deployment in:
MS Access Developer Center however, this change prevents editing tables in datasheet view, so I suggest adding it in a function and calling it on/off as needed.
Public Function SetDesignChange(chnage As Boolean)
CurrentDb.Properties("AllowDatasheetSchema") = change
End Function

Disabling Design View in MS Access

I'm trying to input some code that hides "Design View" as an option for our internal application unless a certain permission requirement is met.
The following code works with one exception:
On Error Resume Next
If Not GetUserInfo("ADMIN_PERMIS") = 1 Then
Dim cb As CommandBar
Dim cbCtl As CommandBarControl
For Each cb In CommandBars
If cb.type = msoBarTypePopup Then
For Each cbCtl In cb.Controls
If cbCtl.Caption = "&Design View" Then
cbCtl.enabled = True
cbCtl.visible = False
Else
cbCtl.visible = True
End If
Next
End If
Next
Set cb = Nothing: Set cbCtl = Nothing
End If
The one problem with this is that it disables Design View not only for the current database, but also for any other access database that is launched. I'm looking for a way to try and apply this code in such a way that it only affects the Access database I have the code in and not in every single instance of it.
I recommend converting the database into a compiled, executable only .accde file (File --> Save & Publish --> Make ACCDE). Doing this will prevent any design or code changes in the application. Keep a development version in the normal .accdb format. Make your changes there and then compile into the .accde version for each update.
Since your team updates the database often, you could benefit from using Peter De Baets' database starter. The database starter makes a local copy of the front end of the database, allowing uses to continue to work while design changes are being made. After the production accde front end file is updated, the users will automatically copy the new file the next time they open the database. In my office I have found that I can push out a quick fix and simply email everyone saying "Close and reopen the database guys!".
All these answers are great. If you are interested in the simplest method, I found the form holds the key, albeit in a strange spot.
In the forms properties->Other Tab->Shortcut Menu =No

Running VBA code on an event in an Access web database

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.

How to open locked app in design mode?

Our MS Access 2000 developer left the company a year ago. We need to open his app in design mode in order to make modifications. Double-clicking the .mdb file while holding the shift key doesn't work. When I do that the developer toolbar shows for a split second, then all toolbars go away and the app opens as users would see it. No toolbars show and only a basic dashboard is visible to run the app. I tried using the password recovery tool mentioned here but the tool says there is no password. Can someone tell me how I can open this app to make code modifications?
Beth's answer worked for myself and a co-worker. I've copied the solution here from the link in case the link dies.
"...To unlock the Access DB you can use the following if you know the full path to your database.
Copy the following function into a module in ANOTHER database and call the function. You will need to set the strPath to the path of your database."
Public Function ResetExternalDatabaseBypassKey _
(Optional tfAllow As Boolean = True)
'Name: ResetExternalDatabaseBypassKey (Function)
'Purpose: Sets AllowByPassKey to true in another Access Database
'Author: John Spencer UMBC-CHPDM
'Date: May 02, 2000, 12:08:19 PM
'*******************************************
Dim dbs As Database
Dim strPath As String
On Error GoTo ResetExternalDatabaseBypassKey_ERROR
strPath = "C:/MyDatabases/SomeDataBaseName.mdb"
Set dbs = DBEngine.Workspaces(0).OpenDatabase(strPath)
dbs.Properties("AllowByPassKey") = tfAllow
ResetExternalDatabaseBypassKey_ERROR:
Select Case Err.Number
Case 3270 'Property not found
MsgBox "Allow Bypass Key property does not exist " & _
"for the chosen database."
Case Else
MsgBox Err.Number & ": " & Err.Description
End Select
End Function
Make sure there wasn't another copy left around without this code enabled.
This is typical code used to do this sort of thing, SetByPass There are instructions to hit Cntl-G to open VBA Editor and run code to unset this thing.
Have you tried creating a new blank database, and then importing everything?
It sounds like the shift key by-pass has been set, but that's as noted usually only needed for deployment. In fact, for any access developer, this is quite much a standard approach.
So, it not clear if anything at all been locked up here or the person just set things up as most access applications should be when deployed to users.
About the only thing you lose when importing to another application is the tools startup settings and the shift key. If there are custom menus, make sure you check the option to import those also. (However, be careful, as sometimes custom menus can be setup to run a macro on startup, and this was an old security trick). So, if importing once with tool bars and holding down shift key still causes startup code to run, then try creating another blank database and this time import everything except the menu+tools bars from the old application. Holding down shift key will thus then work for you.
Also, if the system not asking your for a password, then what would you expect a password recovery program to do? All you need is basic access developer skills here to deal with this problem and throwing things like password removal when you not being prompted for passwords will not move you forward here.
On the other hand, if after importing all the forms into a new database the design ability is grayed out, then this was in fact a mde file, and you are in a rather difficult situation unless you can find the original mdb file used to create the mde.