So I have this Access 2010 database into which users must login. The username they use is saved as a global variable. I then have a form which updates a table when they enter data and click a "save" button. I am trying to set one of the comboboxes (user) that is currently linked to a column in the table to be filtered on the global variable so that each person can only enter data under their own username. Is this possible? Does anyody know how to code this? I'm a complete newbie to Access and VBA and would appreciate any help
Greets
Me
In the form_load() function of that form you should fill the combobox with the global variable. To be sure they can't edit you should disable the combobox as well.
Private Sub Form_Load()
Me.myComboBoxName = gMyGlobalVariableName
Me.myComboBoxName.enabled = false
End Sub
However I'm assuming that the combobox has two columns (id and username) of which the first one is hidden and the primary key of some table where you store all the usernames. The gMyGlobalVariableName should have stored the id, not the username itself.
You can set the row source of the combo in the load event of the form to include only the relevant rows.
Me.TheCombo.RowSource = _
"SELECT UserColumn, Etc FROM TheTable WHERE UserColumn ='" _
& TheVariable & "'"
You may also wish to ensure that the form only contains the relevant records, however, the fact that you have a save button, suggests an unbound form. In Access, save buttons are largely redundant because the default is to save a record and stopping saves is the difficult bit.
I wonder why you do not use their windows log-in user name?
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.
I used the following VBA Code in The Form (General)
Public Function GetUserName() As String
GetUserName = CreateObject("WScript.Network").UserName
End Function
The Control source = User_Name
Default Value = GetUserName()
The Problem -
The username is correctly Pulled in the form, however it is not being saved in the Control Source i.e in the Table. Even after I save and close the form.
I need the form to capture and save the Username every time someone makes changes to a record.
Please Help I am new to MS Access
It looks like you are assigning the user name to the control source. Access does not work like this. The control source is supposed to contain a column name.
Create a table having columns you can use to store things. Then set the form's Record Source property to this table name at design time in the properties window. Then set the Control Source of a text box to the column name where you want to store data or add objects to the form using the fields list.
Iin the form load event you can assign a user name to this textbox with
me!theTextBoxName = GetUserName()
You can also do this with an unbound textbox, but this name will not be stored when you close the form.
But as #June7 points out, you have probably done this already. In this case you should open the form with
DoCmd.OpenForm "theFormName", DataMode:=acFormAdd
... to create a new record.
Is there some sort of work-around that could make this possible? Or could anyone offer just some general advice on my situation below? I tried to set the record source through VBA but had no luck.
My situation:
I have a main form with a subform contained within, and I want the subform to be purely for data entry (Data Entry = Yes). Upon clicking a button, the user is sent from the main form to a report (print preview only). I want the source for this report to be the subform the users are entering data into, but I don't have this option from the report's property sheet itself (no forms), and I also was unable to set it through VBA. Here's my code, it's one line so I highly doubt it's the problem.
Private Sub Report_Load()
Reports![P18003 SDR Report].RecordSource = "P18003 SDR Subform"
End Sub
Previously, to work-around this I had a parameter query that waited for the user to enter data into an unbound textbox from the main form, and an after update trigger on that textbox would load any data relevant to that parameter (my employer and I miscommunicated the requirements).
It turns out though that I don't need to load any older data, and I run into problems with my current parameter query in the event that the user does input a parameter which loads old data - that old data is transferred to the report in addition to the new data they've just entered. This is the main problem with the method I am currently using, and it trashes almost all functionality with this form and report. The events that a user would need to enter a parameter which queries older data are few and far between, but it's not a functional product unless I can get the subform to be connected to the report directly. There's likely something obvious I'm missing here (or at least, I hope there is).
Thanks in advance for taking the time to read this and for any help you may offer.
you can either send the recordsource to the report itself in the OpenArgs by the open report event, after the click event in the subform
Private Sub btnSubform_Click()
Dim strSQL as String
strSQL = "SELECT * FROM SubformQuery WHERE ID = " & CurrentSubfromRecordID
Docmd.OpenReport, acViewReport, , , , strSQL
End Sub
and then in the Report.
Private Sub Report_Open(Cancel As Integer)
Me.recordsource = Me.OpenArgs
End Sub
Or you can make a paramter Query and set this Query criteria as record source for the report. So the parameter in the query is pointing to the current selected row in the subform. Like this:
--- At the Criteria in the Query designer:
--- RecordSource for the Report:
Forms![FormMain]![YourSubform]![ID]
Now every time the reports obens he gets the ID from the current selected record of your subform.
I am creating a small Access DB for our Data Entry guys. Our main DB is mysql but due to ease of use we are creating an Access DB to make it easier for them to enter the Data. I have done most of it but am stuck with this problem (which I know how to solve in mysql+php) Please pardon my ignorance, but I have just started using MS Access.
I have two tables - ClientPhones and sales. The ClientPhones table has phone, clientid fields. sales table has salesid, clientid, date, etc fields.
I also have a Form which has all relevant fields for the sales table. I want to add a new field, phone_no in that form. When a user inputs the number and on focus lose event, I was access to run a query on the clients table to see if the phone number exists in any of the 3 phone number fields. If it finds a client with that phone number, the client ID should be populated, else a new form to input the client details should be shown.
Is this possible with MS access or am I doing this incorrectly?
Use the text box's After Update event to retrieve the clientid which matches the phone number the user entered.
If a clientid is found, store it in the text box which is bound to clientid.
If no match is found, ask whether the user wants to add a new client, and open that form if they respond yes.
This code outline assumes txtSearchPhone is the name of the text box where the user enters the target phone number, and txtClientId is the name of the text box where you want to store clientid.
Private Sub txtSearchPhone_AfterUpdate()
Dim varClientId As Variant
Dim strCriteria As String
strCriteria = "[phone]='" & Me.txtSearchPhone.Value & "'"
Debug.Print strCriteria '<-- inspect this in Immediate window; Ctrl+g will take you there
varClientId = DLookup("clientid", "ClientPhones", strCriteria)
If IsNull(varClientId) Then
If MsgBox("Add new user?", vbYesNo) = vbYes Then
'DoCmd.OpenForm ... (see Access help topic)
End If
Else
Me.txtClientId.Value = varClientId
End If
End Sub
Make sure the text in txtSearchPhone does not include a single quote character (') because the DLookup will break if it does. You can use the text box's Validation Rule and/or Before Update event to make sure a single quote is not present.
I have a form that will display the last record of a table, with a few additional fields from other tables. The PK of the main table is auto-numbering. I need to be able to allow the user to make a change to the auto-numbering field, then determine if the record exists or not, if so - update, if not - insert new. I tried adding an event to the field on field change but whenever I try to click in the field on the form, it just beeps at me. Here is the code:
Private Sub JobID_Change()
'Check Bid#, if already exists open selected record for editing
rstOpenOrder.FindFirst "JobID = " & Me![JobID]
Do Until rstOpenOrder.NoMatch
With rstOpenOrder
'Add new record to end of Recordset object.
.Edit
'Edit data.
!LocationID = Me![LocationID]
!Description = Me![JobName]
!BaseBid = Me![BaseBid]
!GrossMargin = Me![GrossMargin]
!MDs = Me![ManDays]
!BidDate = Me![BidDate]
!ShortDate = Me![ShortDate]
!EmployeeID = Me![EmployeeID]
!GC = Me![GCs]
.Update
.FindNext "JobID = " & Me![JobID]
'Save changes.
End With
Loop
End Sub
If anyone can help, I would greatly appreciate it!
It's not generally a good idea to expose autonumber fields in the user interface. However, if you still want to provide the interface you've described you'll need to do the following:
Add an unbound text box (I'll call it IDLookup)
Add an AfterUpdate event to the text box to check for an existing record
If the record exists, filter the form to display that one record
If not, move the form to a new record (Me.Recordset.AddNew)
The key here is that your lookup must be done via an unbound text box. Access won't let you edit anything inside a textbox bound to an AutoNumber field (for good reason!).