MS-Access - Open a form from a hyperlink data click - vba

I have a table and number of forms in my access database.The table has a column of type hyperlink. When i click a value in this column, one of my form in the database should be opened .
How can i proceed?

First off, you shouldn't give your users access to the tables in your database.
Create a form, in datasheet view, based on the table. This allows you to control what your users can do with the data; under Form Properties -> Data you can choose whether users can add, edit, delete and filter the records.
The second form, which is to be opened from the first, should be set up so that the user cannot add records or move between them; ie. no Record Selectors and no Navigation Buttons.
I would advise against using the Hyperlink field type for this, it is not what it is designed for. The Hyperlink field type is for linking to external files, either local or online -- not for navigation around the current database. Use whatever field type best suits the data in the field.
Instead, I would assign an On Dbl Click event to the field you wish to use for linking to the form. Open the datasheet form. Switch to design view, open the property sheet for the form and set HasModule (under Other) to Yes to allow use of VBA on this datasheet form.
Open the property sheet for the field, and under Event click the three dots by On Dbl Click, then Code Builder.
We now must add code to open the other form (showing a single record) at the correct record. This will look something like the following:
Private Sub [field to double click]_DblClick(Cancel As Integer)
'When [field to double click] is double clicked, open [single-record form name] at record
DoCmd.OpenForm "[single-record form name]", , , "[unique field] = " & Me![unique field]
End Sub
Two things should be noted here:
The field to be double clicked does not have to be the field that the record is selected with. In fact, you could put similar code on each field so that, wherever the user double clicks on the record, the other form is opened.
The [unique field] must be unique to each record for this to work properly. I'd use an autonumber ID field.

Related

Open a form to a record selected in a subform

I want to open a form to the record selected in the subform of a different form. I've tried following what I’ve seen in other posts about this, but I still can't get it to work. I think I'm just missing some very basic detail but this is my first Access database and I can't figure out what it is. Thanks in advance for your assistance.
Details:
F_Detail - (This is a Single Form containing details on a project.)
F_List - (This is a Single Form containing a subform and a button.)
subF_List - (This is the subform contained in F_List. This subform is in Datasheet view)
Project_ID - (This is the primary key contained in subF_List and in F_Detail. It is the common criteria between the two. It is Short Text type.)
subF_List displays row after row of projects. F_Detail displays details about a single project at a time. From F_List I want to select a row in subF_List and then click the button to open F_Detail, and F_Detail will be displaying details of the project whose row was selected in subF_List when I pressed the button.
What I Have for the Button:
On Click > Event Procedure
Private Sub ProjectDetailButton_Click()
DoCmd.OpenForm "F_Detail",,,"Project_ID = " & Me.Project_ID
End Sub
Upon clicking the button, I get an error saying, "Compile error: Method or data member not found" and it highlights the .Project_ID at the end of my code.
I don't know what I'm doing wrong and would appreciate any help you can lend. Please let me know if I've left out any needed detail.
Use apostrophe delimiters for text field parameters. Use # for date/time, nothing for number.
"Project_ID = '" & Me.Project_ID & "'"
Me is alias for form or report the code is behind. To reference a field in its RecordSource:
Me!Project_ID
Code on main form referencing field on subform must reference through subform container control. I always name container different from object it holds, such as ctrProjects:
Me.ctrProjects!Project_ID
I name controls different from fields they are bound to, such as tbxProject:
Me.ctrProjects.Form.tbxProject

MS Access one form to select table values and use to populate multiple fields on other form?

I have a table called GL_Account and IM_Productline
The table IM_Productline has various fields that need to be populated with a value from the field GL_Account.AccountKey (i.e. IM_ProductLine.InventoryAcctkey and IM_ProductLine.CostOfGoodsSoldAcctKey)
To populate the IM_ProductLine table I made a form "Product Line Maintenance" with all the fields. To populate the field IM_ProductLine.InventoryAcctkey I put a (magnifying glass) button behind the field with the following code:
Private Sub CMD_Select_GL_Account_Click()
Me.Refresh
If IsNull(Select_ProductType) Then
'do nothing
Else
Forms![Product Line Maintenance].InventoryAcctkey = Me.SelectGLAccountKey.Column(0)
Forms![Product Line Maintenance].Refresh
End If
DoCmd.Close
End Sub
So the button opens a form Called "Select GL Account" with a combo box that enable to SELECT GL_Account.AccountKey, GL_Account.Account, GL_Account.AccountDesc
FROM GL_Account; and when the OK button is clicked it writes the value from GL_Account.AccountKey to IM_ProductLine.InventoryAcctkey, closes the form "Select GL Account" and then refreshes the form "Product Line Maintenance" so the account number and description become visible for the user.
This all work fine but here's my question:
Now rather than creating a new form for every account field I need to populate (i.e. "Select Inventory GL Account" select "Cost Of Goods Sold GL Account" etc) I'd prefer to use the form "Select GL Account" to select and populate the 11 different account fields. So behind each xxxAcctkeyfield on form "Product Line Maintenance" is a (magnifying glass) button that when clicked pulls up the form "Select GL Account" and when "OK" is clicked it writes the selected AccountKey to the correct field on form "Product Line Maintenance"?
I'd greatly appreciate anyone's efforts to understand what I am trying to explain and point me in the right direction.
Ok, there is the issue that all 11 fields should not require to be "copied" since you have a relational database (you would ONLY store the row PK ID of that selection in the current report. (a so called FK (foreign key) value). That way, say you want to change the choice? Well then you could pop up that form - search + select the one record with all that information, and then upon return ONLY store the one value.
So, I would give some thoughts to the above - you want to leverage the relational database features. And as a result, you don't need to "copy" all that data. This is not much different then say creating a invoice. I can create the new invoice, but all of the address information, and the customer that this ONE invoice belongs to? Well, that is one column with a FK value that points to the customer. Once I select that one customer, then display of the customer name + address can be say a sub form or some such - but no need exists to "copy" that information. It would also means with near zero code, you could move a invoice between customers!!! - (just change the one fk column with to the new/different customer ID (PK) value.
Now, back to the question at a hand?
You can certainly pop up a form, let the user select, enter, pick and do whatever. And THEN you can have the calling code grab + pick out the values from that form.
The way you do this? It involves a not too wide known trick.
The code that calls the form can simply open that form as a dialog form. This will HALT the calling code, the user does whatever, and when done the calling code will THEN continue. Not only does the calling code continue, but it can get/grab/pull/take any values from that pop up form WIHOUT having to use global vars.
The approach is thus thus:
dim strF as string
strF = "frmPopAskInfo"
docmd.OpenForm strF,,,,,,acDialog
' above code waits for user input
if application.AllForms(strF).IsLoaded = true then
' user did not cancel, get values from form
me!AccountNo = forms(strf)!AccountNumber
etc. etc. etc.
docmd.Close acForm,strF
end if
Now the only other issue? Well, the "ok" button on the popup for DOES NOT close the form, what it does is set visible = False. This will kick the form out of dialog mode.
me.Visible = False
So, if the user hits the cancel buttton (close form) or hits the X upprer right (close form), then the form will NOT be loaded when your calling code continues. But, if they hit OK button, then you don't close the form, but ONLY set visbile = false.
This allows the calling code to continue, you are free to get/grab/take values from that form, and then once done, you close the form.
So a form close in that popup = user canceled the form.
So, a popup form, and even a modal form? They do NOT halt the VBA calling code, but a acDialog form does!
You can thus place 2 or 5 little buttons that pops up this form, the user can pick/choose/select/enter values. When they hit ok, your calling code continues, and you are free to pull values from that form. So while all 3-4 buttons might pop up that form, each individual button launch of the form can have code that follows and updates the given control the pop button was placed beside.

Use command button to open selected record on a form without filtering?

I have a continuous form which displays a small amount of data from each record in my table ProjectT (i.e. project name, status) and a command button in the footer which I would like to open the selected record in its expanded single form (where all of the relevant info is displayed).
At first I set this button up using Access's wizard, but realized that Access opens a selected record by filtering the data on the form. The problem with this is that once the expanded form is opened, I want a user to be able to move to other records without having to select to unfilter the results. If I change the button on my continuous form to simply open the expanded single form, is there code I can run to make the form open to the selected record without putting a filter on?
Initially I thought to set the expanded form's (named ProjectF) default value to Forms!ProjectListF!ProjectID (where ProjectListF is the continuous form and ProjectID is the autonumber primary key for ProjectT), but this was not successful, I think because there is more than one ProjectID displayed on ProjectListF.
Another thing to consider is that I have another button on my Main Menu form which opens the ProjectF form in data entry mode to prevent the user inadvertently changing/deleting an existing record when they are trying to add a new one; I have no idea if this might be important when trying to find a solution to my issue.
I'm open to any suggestion--I have an okay handle on SQL, and have delved into a little VBA but am completely self taught. Any ideas? Thanks!
You can open the detailed form with this command:
DoCmd.OpenForm "ProjectF", , , "[ProjectID] = " & Me!ProjectID.Value & ""

How we can grab value from list box and fills automatically related text box in new form

I have a list box and its row source is "tblitems" with bellow fields.
Now I like when right click on this list box and select one option for example "new task" it opens new task form and automatically Grab the item number from a list box and fills related text box "item number" in "new task" form that is bounded to table "tbltask"
Now when I press "Apply" button it insert new record in "tbltask".
tblItems
item number (pk)
item name (text)
tbltask
task number (auto number,pk)
item number
enter image description here
Getting a value from a list box in a form is:
Me.List_Box_Name.Value
Or if you have unbound values then get it based on the column:
Me.List_Box_Name.Column(2)
or whichever column you need.
You can then populate fields with DLookup or a recordset. Then if you want this to happen when you open a new form, you may want to look into this:
Private Sub Form_Load()
'Stuff you want to happen when that form loads
End Sub
Updated
The following will print out the value each time you click it. You can use this method to trigger a new form to open, or you can have a user click a submit button afterwards.
Private Sub Test_List_Click()
Debug.Print Me.Test_List.Value
DoCmd.OpenForm "Form_Name"
End Sub
Now I'm not exactly sure the best way to add your variable to the opened form. If it were me with my limited knowledge, I would add a global or public string and have the Form_Load() check to see if that string length is greater than 0. If yes then it will populate the field.
Hope that helps
Update 2
Actually this link will help you populate a field from previous form:
MS Access - open a form taking a field value from a previous form

Access 2007: Filtering a report's results using a drop-down box

My question is twofold.
I have around twenty assorted tables in a database. The table layouts are diverse; the one common thread is that all of them have a 'County' field.
I need to set up a series of reports which allow a user to select a county from a drop-down box, triggering the report to run and return only records attached to that particular county.
This is doable at the datasheet level using a filter-by-form, but that's pretty clunky and I have several tables/queries which will need this same county filter.
I may be halfway there with the following:
Create an unbound form.
Add a combo box.
Set the Row Source of the combo box to include the County field.
Set its Bound column to 1.
Set its Column Count property to 2.
Set the Column Width property to 0";1"
Name the Combo Box 'ChooseCounty'.
Add a Command Button to the form.
Code the button's click event as follows:
(Note: To write the code, in Form Design View select the command button. Display the button's property sheet.
Click on the Event tab.
On the On Click line, write:
[Event Procedure]
Click on the little button with the 3 dots that appears on that line.
When the code window opens, the cursor will be flashing between two already existing lines of code.
Between those lines, write the following code.)
Me.Visible = False
Close the Code window.
Name this form 'ChooseCounty'.
In the Query that is the Report's Record Source [County] field
criteria line, write:
forms!ChooseCounty!ChooseCounty
Next, code the Report's Open event:
(Using the same method as described above)
DoCmd.OpenForm "ChooseCounty", , , , , acDialog
Code the report's Close event:
DoCmd.Close acForm, "ChooseCounty"
When ready to run the report, open the report.
The form will open and wait for the selection of the Company.
Click the command button and then report will run.
When the report closes, it will close the form.
I can persuade the report to trigger the form, but only once - I can't seem to figure out where precisely the 'forms!ChooseCounty!ChooseCounty' needs to go. Perhaps someone can clarify or offer a more elegant way to do this?
I need to set up a large meta-report containing sub-reports on all of the tables - and, using the same drop-down 'choose a county' form, I need to have that choice cascade down through all the subreports. I don't have the faintest idea how to go about this. Suggestions welcome!
~ T
You seem to be asking two questions, the last of which is clear to me, but the first is not. The second one is in regard to how to cascade the filter to the subforms. You can do this in one of two ways:
put the form control reference as criterion in the recordsource of each subreport, OR
create a non-visible control on the report that has as it's controlsource "=Forms!ChooseCounty!ChooseCounty". Name that control "CountyFilter". Then, add CountyFilter to the link properties. If, for instance, you are linking the subreports on ID, you'd have:
LinkMaster: ID;CountyFilter
LinkChild: ID;County
(assuming, of course, that ID is your link field for the child reports, and that "County" is the name of the field in the child subreport).
Now, I'm wondering why you would have the County data not just in the parent record but in the child records -- that makes no sense. If you do have it, then the solution above will work.
If you don't, then I don't understand the question, as the whole idea behind subreports is that they are filtered by the parent record, so if the parent record is a person, and you filter by COUNTY, you're only going to get the child records in the subreport for that person, which by definition are already filtered by COUNTY because the parent has been filtered.
As to the earlier question, you write:
I can persuade the report to trigger
the form, but only once - I can't seem
to figure out where precisely the
'forms!ChooseCounty!ChooseCounty'
needs to go
You have two choices:
hardwire the recordsource of the report to use the form control reference, so the WHERE clause of your report would be "WHERE County=Forms!ChooseCounty!ChooseCounty" (and you should set this as a parameter of type text to insure that it gets processed correctly).
the better meethod is to set the recordsource in the report's OnOpen event.
After you open the form as a dialog, you'd have something like this:
Me.Recordsource = "SELECT * FROM MyTable WHERE County='" _
& Forms!ChooseCounty!ChooseCounty & "'"
And immediately after that line, you can close the form, since it's not needed any longer.
You will likely want an OnNoData event for the case where no records are returned. This is usually something simple like:
MsgBox "No records found!"
DoCmd.Close acReport, Me.Name
I hope this answers your questions, but if not, I'm happy to offer more explanation.