Access VBA: Get Recordset from an embedded form - vba

I am trying to access a value within a continuous form, and then pass that to a new form that is opened. I got this working properly, but it only works if the original form is open directly. It fails if I try and run this when the form is embedded within another form.
The error I get is;
Run-Time error '2450': MS access cannot find the referenced
'ViewerForm'
The code I am using is; (courtesy of here: Get Control or Recrdset Value on Continous Form)
Dim r As DAO.Recordset
Set r = Forms![ViewerForm].RecordsetClone 'Clone the recordset
r.Bookmark = Forms![ViewerForm].Bookmark 'Navigate to the active record
Myvalue = r!TagMain.Value
Why does the code work when I am opening the form by itself, yet fails when it is embedded within another form? Do I have to tell it the path through, to find the embedded form, such as
Forms![FormTheViewerIsEmbeddedWithin]![ViewerForm].RecordsetClone
Instead of
Forms![ViewerForm].RecordsetClone

ViewerForm is the subform control on the main form. The control has no RecordsetClone.
You need the (sub)form that's inside it:
Forms![FormTheViewerIsEmbeddedWithin]![ViewerForm].Form.RecordsetClone
If you get the error MS access cannot find the referenced 'ViewerForm', your subform control has a different name than the subform. Check its properties in the main form.

Related

What is a programmatic method to remove attachment(s) on a Access Form's Attachment Control?

I am creating a Form on Access. This Form has source controls to an underlying Access DB. This Form is ONLY INTENDED for insertion of a new record into the DB (No intention of modifying any of the DB's existing data).
In the Access DB, there is a field for Attachments. And on the Form, there is an Attachment Control that is Source Controlled to the Attachments field in the Access DB. The Attachment Control allows users to select files and attach to the form's current record.
I have tried closing the Form and re-opening, but that means the form goes through its life cycle, and does the BeforeUpdate()+AfterUpdate()! This means that the data on the Form that was just recently closed, was entered into the DB. I don't want that!
For the reason that this Form is only for creating new records into the DB, I want a button that can wipe the current Form clean, without the Form doing its BeforeUpdate()+AfterUpdate() methods, which means I can't close the Form and re-open it. I have got most of the Controls on the form figured out in terms of giving it default values. My only problem is resetting the Attachment Control.
What is a programmatic way (In VBA or some other language, DAO?) to remove entries on this Attachment Control (Not the Attachment Field in the DB, but the actual data on the Form currently being used)?
I am aware that in BeforeUpdate(), you may cancel the form's update by setting its Cancel = true in the definition of BeforeUpdate(). But there should still be a way to programmatically deal with Attachment Control (Not the attachment field, I know DAO can handle that easily), it just seem stupid not to have a way for it.
(EDIT) Addon 8/30/19:
It's a better idea to store the pathname of the file in your Access DB rather than the attachment it self, and then do a file copy from one location to another (Looks at the code below as my example). And this is exactly what I did, so I don't have to deal with the attachment control. I have found this control to be too limiting in terms of the programmatic-actions that it can offer to developers.
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
On Error GoTo DebugMsg ' DebugMsg is where I defined what happens on Error
fso.CopyFile srcPath, dstPath, False
The attachment field holds a subrecordset of type Recordset2, so you must deal with it as such:
Private Sub DeleteCurrentAttachment()
Dim Records As DAO.Recordset
Dim Attachments As DAO.Recordset2
Set Records = Me.RecordsetClone
Records.Bookmark = Me.Bookmark
Set Attachments = Records!YourAttachmentFieldName.Value
While Not Attachments.EOF
Attachments.Delete
Attachments.MoveNext
Wend
Attachments.Close
End Sub
Open the form in design mode.
Then in the Form property's > in Data Tab > set Data Entry to True.
add Event to the form
Private Sub Form_AfterInsert()
DoCmd.RunCommand acCmdRecordsGoToNew
End Sub
All this action will insure that you are all the time in new record mode .

Access VBA unable to refresh a form

I have a form (frmDropDownEdit) that has a filtered table as the data. A "New" button is created that opens another form (frmDropDownNew) and the user can enter new data. When complete the new form is closed and the user is back to the original form. The code for frmDropDownNew correctly add the info to the table, then the code refreshes the frmDropDownEdit form but it does not refresh. If I click the refresh button in the ribbon, it also does not refresh. But refresh all does work.
How can I have my code refresh the data in frmDropDownEdit. I also put code me.refresh on the OnGotFocus event but that does not even run.
Here is my source code
Private Sub Command5_Click()
'Add Button
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblDropDown")
rst.AddNew
rst!DdCategory = Me.txtCategory.Value
rst!DdDescription = UCase(Me.txtDescription.Value)
rst.Update
rst.Close
DoCmd.Close
Forms!frmDropDownEdit.Refresh
End Sub
On my MS Access 2010 ×64 single user PC, Forms![AnyForm] .Refresh never worked in VBA, independently where it is placed in any database's code. Form_Current() doesn't run either as it should after data are modified (verified by putting a Stop therein). Moreover, records with modified data are neither marked dirty nor refreshed before the vba code has finished. Procedures which should run without delay when data are modified don't run, even when placed into the modified fields' events.
Thus, one has to use a work-around. Many people recommend to use .Requery instead of .Refresh and then to return by vba code to the desired record, but this requires a field with a primary key.
My solution for tables without primary key is the following:
'…
' ESSENTIAL: this code must be run from ANOTHER module !
' (it runs without error in MyForm's own code [eg. in Form_Activate(),
' but then MyForm is NOT immediately refreshed as desired,)
' one still has to repeat these steps by hand afterwards…
DoCmd.GoToRecord acForm, "MyForm", acNext
DoCmd.GoToRecord acForm, "MyForm", acPrevious
' NB: Forms![MyForm].Refresh doesn't work
' at this place in "MyOtherModule" either.
'…
As mentioned in above code comments: this code must be run from another module ("MyOtherModule") - in my case, a form-independent procedure called upon closing a pop-up form opened from the first form, which interactively modifies data. These data should be updated/refreshed immediately when closing the pop-up form, reflecting all changes and their consequences (for example, automatic filling-in/deleting of other data and/or en/disabling controls or making them [in]visible, depending on the modified fields' values).

Microsoft Access- launch hyperlink contained in data table via button located on form

Using SQL or VBA, what is the code I would need to launch a hyperlink which would go to either the internet or a network location? The form has multiple buttons which would connect 1:1 with the first column in the table. The second column in the table would either have an "http://www.internet.com/databases" or "\network7\HH\forms\hrl\".
Only the admin would have access to the table, and the end-users only have access to the forms. This allows all the users to click on the button and have the website or network location launch automatically, without having the ability to edit the table, which the admin would have.
Within "OnClick", I cannot simply type "FollowHyperlink(http://www._____) because the location (written in the table) would be changing monthly and the admin would have 0 coding or technical background.
The button would not be opening a table or showing the user what is happening, it would only launch the link and remain on the same screen to allow them to click on another button
Thank you in advance,
JT
so far i have:
Dim hyperlink As String
hyperlink = Nz((DLookup("Field2", "sources", "[Field1] = SAP"),"")
If Len(hyperlink) > 0 then
FollowHyperlink (hyperlink)
End If
You have to get the data out of the table and then open it as a hyperlink. so something like this:
Dim rs as recordset
Dim hyperlink as string
set rs = currentdb.openrecordset("Table")
rs.movefirst
hyperlink = rs![RelevantColumn]
rs.close
set rs = nothing
FollowHyperlink(hyperlink)
-----------------------------------EDIT
We found the final solution in the comments:
Private Sub Criteria_Click()
anything3 = DLookup("TargetField", "sources", "[CriteriaField] = 'Criteria'")
FollowHyperlink anything3
End Sub

Leaving a Project file open after retrieving it with GetObject

What is the right way to leave an MS Project file opened with GetObject() open and visible to the user after the end of a macro in a different app?
The information I found online suggests that setting the Application.UserControl property to True before the objects go out of scope should allow the user to continue using the opened file. However, for MS Project at least, the Application.UserControl property appears to be read-only. Is there a way to work around this?
A simplified example showing the problem:
Sub AddTasks()
Dim proj As Object
' Already have the file path from another part of the workflow
Set proj = GetObject("C:\projtest.mpp")
' perform some calculations and add new tasks to project
proj.Tasks.Add "additional task"
' Leave Project open and visible for the user
proj.Application.Visible = True
proj.Application.UserControl = True ' Gives "Type Mismatch" error
' without the UserControl line, runs ok, but Project closes after the end of the macro
End Sub
Instead of using GetObject, could you create an instance of the application and open the project file in the instance?
Sub AddTasks()
Dim msProj as Object
Set msProj = CreateObject("Project.Application")
msProj.FileOpen "C:\projtest.mpp"
'do stuff to project file here
msProj.Visible = True
End Sub
Something like the above (I can't test the above code because I don't have MSProject, but similar code works for MSWord)
For Project UserControl just indicates if the user started the application or not; it appears to be read-only because it is. I've not done what you're asking for with Project, although here is a similar example for Word trying to see and find running instances of Excel. Perhaps this helps a little:
can-vba-reach-across-instances-of-excel

How to find out Activex Controls from MS Access DB forms using vb.net

I am developing a Tool in vb.net and need find out Activex Controls from MS Access DB forms. I am able to conut number of controls in form, but unable to get the Activex Controls only from the form. Can any one have any idea how to achieve this, please suggest.
Can you access the controltype property? If so, I cannot help with vb.net, but here is some VBA that may help.
ActiveXCount = 0
For Each ctl In Screen.ActiveForm
If ctl.ControlType = 119 Then 'Custom control'
'Debug.Print ctl.Class'
ActiveXCount = ActiveXCount + 1
End If
Next
I am not sure what your are wanting is possible.
Try this: go into MS access and create new property that is the count of controls on the form. In VBA, me.Countrols.Count. Open the form using Access automation. OnFOrmLoad() write the number of controls to text file along with name of the form and then close the form.
Afterwards open the text file in VB.net. It is indirect but it would work.
How To Automate Microsoft Access From Visual Basic .NET
To automate:
Dim oAccess As Access.Application
' Start a new instance of Access for Automation:
oAccess = New Access.ApplicationClass()
' Open a database in exclusive mode:
oAccess.OpenCurrentDatabase(filepath:="c:\mydb.mdb", Exclusive:=True)
oAccess.DoCmd.OpenForm(FormName:="Employees")