I got query to make some modification that enable user to open multiple instances of the form when they do search data. I got manage to create a new module following Allen Browne guide and now I can open multiple instances of the spreadsheet which is good step forward but I cannot find solution how I can open that specific form.
In module I have got following code:
Public clnClient As New Collection 'Instances of frmClient.
Function OpenAClient()
'Purpose: Open an independent instance of form frmClient.
Dim frm As Form
'Open a new instance, show it, and set a caption.
Set frm = New Form_SurgeriesForm
frm.Visible = True
frm.Caption = frm.Hwnd & ", opened " & Now()
'Append it to our collection.
clnClient.Add Item:=frm, Key:=CStr(frm.Hwnd)
Set frm = Nothing
End Function
And under my button I replaced bit:
DoCmd.OpenForm "SurgeriesForm", acNormal
Forms!SurgeriesForm.Requery
.. and usef following code:
ModuleInstances.OpenAClient
I recon I need to add / change somehow the code below to be able to open multiple instances but do not have idea how to do it.
DoCmd.OpenForm "SurgeriesForm", acNormal
Forms!SurgeriesForm.Requery
I appreciate for any help with this as i got stuck and do not know how I can achieve this.
I think this is what you want?...
With the button on your SurgeriesForm add this code to the On Click event to open a new instance of the form:
Private Sub Command8_Click()
OpenAClient
End Sub
This will create a new instance of the form each time you click the button.
Next, add a blank combo-box to the form and change it's Row Source Type to Value List.
Add this code to the form:
Private Sub Combo9_GotFocus()
Dim frm As Variant
Combo9.RowSource = ""
For Each frm In clnClient
Combo9.AddItem frm.Caption
Next frm
End Sub
Now, when you click the drop-down it will list each form that you have created an instance of.
Finally, add this code to the form:
Private Sub Combo9_AfterUpdate()
clnClient(Combo9.Value).SetFocus
End Sub
Now, selecting a form name from the combo-box will move the focus to that form.
NB: You'll need to update the name of the controls to match yours.
NB2: Remember to open the first form using OpenAClient or it won't get listed in the combobox (as it's not in the collection).
Related
I've created a table and form to track sales opportunities. I'd like users to be able to "connect" associated files on our server to each opportunity. For example, they might like the opportunity to point to price quote.
Since attaching files to the database is a debatable move, I've opt to save the path and would like to use FollowHyperlink to navigate and open the file.
My strategy is to create a subform containing the links associated with a particular opportunity. The user could then click on the subform nick name in the subform to open the associated file.
By surfing the web, I've managed to create a macro allowing the user to store the selected file and path in a column called LinkLocation , assign the entry a nick name via a InputBox, and store the nickname in a column called LinkName. This macro is working as expected.
Edit: Code shared.
Sub test()
Dim f As Object
Dim strSQL As String
Dim strShorthand As String ' Short hand name for display in subform.
Dim strFullFilePath As String ' Full file path
Set f = Application.FileDialog(3)
f.allowMultiSelect = False
f.Show
strFullFilePath = f.SelectedItems(1)
strShorthand = InputBox("Enter the shorthand name here.")
strSQL = "INSERT INTO tblLinks (LinkLocation, LinkName) Values ('" &
strFullFilePath & "','" & strShorthand & " ');"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End Sub
I am having two problems.
When I click to create a new record in the subform linked to tblLinks, my macro runs and the data is correctly stored. However, the newly created record does not show up in the subform.
How do you create a double click event so FollowHyperlink properly engages and opens the file in question?
I cannot find an answer to either of these questions on the web.
I've written a fair amount of complex VBA for Excel, but Access is completely new to me. Any resources you can recommend are most welcome.
Thanks in advance.
Enter values into bound controls on form instead of running INSERT action and issue will go away. Instead of InputBox for LinkName, just enter into textbox on form. If intent is to enter a new item, make sure focus is on new record row. Then code behind subform:
Me.LinkLocation = strFullFilePath
Otherwise, have to Requery or Refresh form to display new record. Again, if code is behind subform, simply: Me.Requery.
As for double click event, you already know how to create VBA code so do the same for this event using FollowHyperlink command. Can be double click of a textbox or single click of command button. Format either to look like clickable hyperlink. Set textbox as locked so user cannot accidentally edit.
What I am trying to accomplish: I have a report built that feeds from a button on a form and the info selected on that form. What I need is a code that will allow the report to change the header caption based on the information selected from the print report button on the form. So essentially on my form I have three fields that are drop downs. When I make my selection on the first two fields and I want a report that lists only that information I hit the button that says print report and it works just fine. What I want it to do is when I hit the print report button it will open the report with the a specific title based on what selections were made from the form. So for example on my form I choose the branch from the drop down then I choose an organization from the drop down. When I hit the print report button I want that report to open with the Title of the branch and the organization. I want it to be able to change each time the selections are changed. Essentially update the information from the form to the title or caption of the report. Any suggestions? I found a code I thought would work but I cannot make it work when I tweak the information. I am fairly new to the coding and access world and would appreciate any help.
Here is the code I was trying to use:I was trying to figure out how to add snipping photos I took but don't know how. Here is the code I was trying to use I solved the problem by passing the new text for the Label.Caption in the "OpenArgs" parameter in the OpenReport command, and used the Open Event for the Report to install the new Caption. It works great!
Code:
>>> In Control Form Module
Private Sub Command0_Click()
Dim aWhere As String
Dim aStrArg As String
'Select a WHERE statement (from Global List) based on the Radio Box Group
aWhere = aDept(Me.Dpt_Chain.Value)
'Pass a String Argument to the Report to install in the Label
aStrArg = "TEST 22"
'Open the Report and pass it a string ...
DoCmd.OpenReport "Price_1", acViewPreview, , aWhere, acWindowNormal, aStrArg
End Sub
>>> In Report Code - Form opens with "TEST 22" in the Label
Private Sub Report_Open(Cancel As Integer)
If Not IsNull(Me.OpenArgs) Then
Me.Controls("Dpt_Label").Caption = Me.OpenArgs
End If
End Sub
I tried to tweak it to work with mine but can't. this is not my information for my stuff but I thought by tweaking this code I could make my caption/title box on my report change each time different information was selected in my form.
Tested, and this worked for me:
Dim strString As String
strString = "Hallllo"
DoCmd.OpenReport "Report1", acViewReport, , , , strString
Private Sub Report_Open(Cancel As Integer)
Me.Controls("Label1").Caption = Me.OpenArgs
End Sub
I have a main form named Calling. It has a button named Travel. That Travel button does:
Private Sub btnTravel_Click()
On Error GoTo btnTravel_Click_Err
DoCmd.OpenForm "Travel", acNormal, "", "", acFormEdit
'If you can find a cleaner way to open a form I will be thankful.
btnTravel_Click_Exit:
Exit Sub
btnTravel_Click_Err:
MsgBox Error$
Resume btnTravel_Click_Exit
End Sub
The Travel info form performs correctly. That Travel form has a Close button with the code:
Private Sub bntClose_Click()
Unload Me
End Sub
When pressed, the Close code generates "Run-time error '361': Can't load or unload this object.
Your help is much appreciated.
You do not need the commas with the empty strings, nor do you need the acFormEdit as when you open the form you will be able to edit and add new records anyway.
If you leave this argument blank the form will open in the data mode set by the forms AllowEdits, AllowDeletions, AllowAdditions, and DataEntry permissions (in the form properties).
DoCmd.OpenForm "Travel", acNormal
As for the next sub routine, I would use docmd.close instead of unload.
Private Sub bntClose_Click()
Me.Undo
DoCmd.Close acForm, "Travel", acSaveNo
End Sub
The me.undo is optional, if you don't want to save, and if you want to save the form change the acSaveNo to acSaveYes.
EDIT:
I have just re-read your question and noticed in the title you want to do this without docmd.
I have had a think about this and docmd is the standard way of closing forms in access using VBA. I am not sure if you have inherited the unload from using VB, but I would stick to docmd.close when using access.
"Unolad me" just doesn't work in Access Form objects.
Access wants you to use DoCmd.Open/DoCmd.Close but you are clever than that and can use Access Form Object as an object. Access names the Form Classes prefixed the name you give them with "Form_". You create a Form named "YourForm", Access create the Class "Form_YourForm". Use this class as a real object:
'Declare a typed variable
Dim f As Form_YourForm
'Create the object
Set f = New Form_YourForm 'This triggers the Open event
'Use the object
f.SetFocus
f.Resize
'... And eventually, dispose the object
Set f = Nothing
'Remember <<Unload f>> won't work, neither you can use DoCmd.Close
Also, you can use Form_YourForm directly as an object variable because VBA Access just create "implicitly" this object out of the class Form_YourForm when you first use it or when you use DoCmd.Open. (Yes, it's a little bit confusing, but Access was create for users that didn't have necessarily programmer skills).
However, you'll get a different instance of the Form_YourForm Class each time you use a variable object typed as any of the Form Class that exist in your project. This means you can open as many instances of a form as you want ...or while it fits your computer's memory. You can't acomplish it using DoCmd.Open.
The main "disadvantages" are that you have to handle the form object from another module. You can still use the "Forms" collection but since you don't know the given key you can't reach your form instance easily. Also, you can't close the form from its own code, but only disposing the typed variable (Set f=nothing).
I want:
To be able to open a form, select an item from a control box, click a button to open a new form and be able to input new records that have the previously selected item added to their fields.
What I have done so far:
I have made two forms, added the controls in both, added a command button.
I have used MS Access wizard to add this code:
Private Sub CommandNext_Click()
On Error GoTo Err_CommandNext_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "FormX"
DoCmd.OpenForm stDocName, , , "[BatchID] = " & Me![ListBatch], acFormAdd
Exit_CommandNext_Click:
Exit Sub
Err_CommandNext_Click:
MsgBox Err.Description
Resume Exit_CommandNext_Click
End Sub
The third line is mine.
As a result:
If the second form isn't already open, pressing the button on the first form will open the second one with empty data (the text field that should show the value of the parameter is empty).
If the second form is already open, pressing the button on the first form will change focus to the second one and it will show an old, existing record that matches the parameter I selected
I want to open the second form without any old records showing but with the selected parameter appearing in the designated text field.
In both cases in the second form the Filter property gets populated with the parameter I've sent using the button, but that isn't what I am aiming for.
Are you aware the 4th argument for the open form command is the where condition or filter to apply?
If you use the last parameter which is called open arguments (OpenArgs) then you can write code in the 2nd forms open event to set the default value for the column in question.
The open form code would look something like this:
DoCmd.OpenForm stDocName, , , , acFormAdd, , Me![ListBatch]
You could then put code similar to the following in this 2nd forms open event:
Dim defaultID as Long
defaultID = CLng(Nz(Me.OpenArgs, 0))
If defaultID = 0 Or IsNull(Me.OpenArgs) Then
Cancel = True
Exit Sub
End If
Me.TextBoxBatchID.DefaultValue = defaultID
Or if you do not need the ID to be set for every new record you could just set the current value of the control to the defaultID variable.
Me.TextBoxBatchID = defaultID
Please note the code above would not open the form if the open argument turned out to be empty or 0.
http://i.stack.imgur.com/NhBss.jpg
I have on the f3AgreeService form the Agreement itmes(parent) and Service details(child). I used a sub table (tService) rather than a sub form to show service info as I want to take advantage of the subdatasheet function that is available only to tables (the + to expand function. The subdatasheets can be linked to either facility/DHB info of the service)(1 to 1-many).
I want to be able to use a button (see facilities/see DHBs) to switch between two different sub data sheets for tService sub form. The code is as follows.
The problem is that sub data sheet will not automatically update, until you close and reopen the whole form. I could for sure close and open the form every time the user clicks. But it seems ugly. Is there a way just to requery or update the tService for the sub datasheet information to take effect?
Private Sub cmdDHBs_Click()
Dim MyDB As DAO.Database
Set MyDB = CurrentDb
MyDB.TableDefs("tService").Properties("SubDataSheetName") = "Table.tServ_DHB"
MyDB.Close
Call RefreshTable 'How?
End Sub
Those I have tried and not worked:
Forms!f3AgreeService.Refresh
Forms!f3AgreeService.Recalc
Forms!f3AgreeService.Query
Forms!f3AgreeService.Repaint
If you really insist on doing this, and I do not recommend it, you can reload the source object of the subform after you have change the subdatasheet:
Me.MySubformName.SourceObject = "Table.tService"