Copy Value from combo list on login screen to username field on new form - ms-access-2007

Newer to access...I am trying to copy the value selected from the combobox on the log in screen to a field on the new form that opens after successful login.
The field I need copied is called cmoemployee on the Login form.I need that to go to the username field on the mainframe form.
Any help would be greatly appreciated! I have tried many different ways but I can't seem to get it.

Haha,
I actually know what you mean here. This can be a complete pain as the variables are deleted when you open a new form In general it is better to show what you have tried so people are able to help you fix it. Anyway, I've tried to do this so I know how frustrating it can be. There are two main ways that this can be done. You can open the form with openargs (I do not think this is a good option for you as you will have to have the openargs constantly transferring from one form to the next and will be cleared if one form misses them.
A better option would be to use tempvars which are not deleted. This is the code I used to do something similar:
I am going to assume your combobox is two columns (but only displays one) and the bound column is the primary key to an employee table. In which case this would work (or you can modify it to meet your needs)
Log in:
'Check Password (or however you check it
If StrComp(txt_Password, DLookup("[Password]", "tbl_Employee", "[ID] = " & cmoemployee), 0) = 0 Then
TempVars.Add "MyEmpID", cmoemployee.Value 'TempVars will store the combobox value
Else
Msgbox "Incorrect Password Please try again"
End If
On Load event of mainfraim form:
[UserName] = TempVars!MyEmpID
That should get you started, so you can build the rest. Let me know if you have any questions.

Related

How can I make a form instance open a specific record on creation?

I'm trying to optimize the performance of my access frontend. One of the problems I'm stumbling on is how to set a multi-window form to open immediately to a specific record, because at the moment it queries everything twice.
Essentially, I allow my users to open multiple instances of a form. That way, a user can compare multiple records by placing the windows of those forms side by side.
At the moment, my code looks like this:
Set frm = New Form_Name
frm.RecordSource = "select * from Table where id = " & ID 'ID is a variable passed to the method
I'm pretty sure back then this question was one of the building blocks I relied on.
The problem is that on the first line, access already entirely opens the form and does everything the form does when opening, such as Form_Open, Form_Current, and loading subforms. Then, when I set the recordsource, it does all (or most) of that again, which significantly slows down the process of opening the form.
Here are some of the things I've tried:
Changing the Form_Current to recognize that it's being "used" twice and only actually run the code once. The problem is that the code is already very optimized and doesn't seem to be the bottleneck, so this doesn't seem to do much. Actually opening the form seems to be the bottleneck.
Trying to change the properties of the original form so that it opens the specific record I need, but I can't seem to change the properties without it opening the form.
Looking for a way to supply arguments to the form. That doesn't seem to be supported in VBA.
One other idea that popped into my mind was creating a variable in vba with the ID of the record, setting recordsource in the form properties to fetch that variable, but then, when I would open another form and change that ID, the form
I realize that I can do this with DoCmd.OpenForm, but that doesn't give me the option to open multiple instances of the same form.
How can I achieve this? This would mean a significant performance improvement.
OK, so I actually wanted to ask this question. But just before I hit the submit button, I had an idea...
Here's the idea: you set a global variable in VBA, which you then access in the form filter command. In my case I just added Public OpeningID As Long at the top of my VBA module. Then I create a function to get that value:
Public Function getFormID() As Long
getFormID = OpeningID
End Function
Then, in the form, you can set the filter criteria as ID = getFormID(). And then when you open the form instance, you do it like this:
OpeningID = ID
Set frm = New Form_Name
OpenindID = 0 'reset this to make sure that if it's being accessed when it shouldn't, it generates a clear error
The only problem is what happens when you refresh the form, as that will call the method again. When you refresh the form via VBA, you can set the OpeningID beforehand, and to avoid users refreshing the form, you can add this to your form (don't forget to turn on Key Previews in the form settings):
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF5 Then KeyCode = 0
End Sub
Bang. Forms open almost twice as fast. Awesome.

MSAccess VBA: Pass values from a subform to a seperate form

I have a form frmDetail that contains several locked fields I want populated only when another form has been filled out, in the name of having some kind of standardization in my data. This other form frmSignOut is used to enter in a date and location that will populate the fields on frmDetail. frmSignOut also contains a subform subULookup that looks up users from a different table using an identifier number. The resulting last name, first name and phone # should also be passed to frmDetail. I also hope to combine first and last name somehow into a Last,First format.
My approach so far has been to open frmSignOut modally with acDialog and I inserted Visible=False into the On_Click event on frmSignOut. Then I try to reference my subform fields and set them to the desired fields on frmDetail. I finish by refreshing and then closing the dialog form.
Private Sub CmdSignOut_Click()
DoCmd.OpenForm("frmSignOut"),,,,,acDialog
If CurrentProject.AllForms("frmSignOut").isLoaded=True Then
Set Forms!frmSignOut!SubULookup.PhoneNbrTxt=Me.ContactNbrTxt
DoCmd.Close (acForm), ("frmSignOut")
Me.Refresh
End If
End Sub
I have only been able to get this far, trying to pull the first field PhoneNbrTxt. I can get frmSignOut to open, I put in data and when I click my 'close' command button I get run-time error 438: Object doesn't support this property or method.
The line highlighted is where I try to reference my subform field. I also have a 'Cancel' button added to frmSignOut that just closes the form, I have no errors there.
Again I'm completely new to Access without much prior experience in anything related, I'd appreciate any knowledge you guys can throw at me.
EDIT: I've been able to successfully pull the value to Me.ContactNbrTxt by adjusting my code to the below.
Me.ContactNbrTxt = Forms!FrmSignOut!SubULookup.Form!PhoneNbrTxt
It looks like the missing part was the Form! right before the control name, along with formatting this correctly and dropping Set.
Im still trying to work out how to combine first and last name before also pulling those to frmDetail, if anyone can help there.

MS Access Database form not updating after SQL requery

I have a report that runs from a query. The query does use a global variable but this is not the problem but needed for the explanation. The function for the variable is:
Function Var1() As String
Var1 = strVar1
End Function
The query where statement is:
WHERE (((IIf([MinOfDueDayMin]<0,0,Int([MinOfDueDayMin]/7)+1))<Var1()+1) AND ((tblEquipment.Retired)=False))
which uses the var1 function
The criteria is on a field that is actually a calculation and that is where I think the problem starts.
The report is run for a command on another form using the following code:
strVar1 = InputBox("Enter Number of Weeks for report")
If strVar1 = "" Then Exit Sub
DoCmd.OpenReport "rptEquipPmSchedule", acViewReport
Everything works just fine
On the report I have a double click event that opens a form. This form uses part of the same query. (not the same one but two levels higher) thiS allow the user to change things so i expect to use requery for the report.
If i double click and then not even change anything and then go back to the report I have #ERROR in the fields that have the calculations
i put a me.requery in the activate event of the report. this did not work.
So I tried a work around.
When I double click the report field, i close the report and send the strVar1 value to the form that is opened. then when I close the form I reasign the strVar1 just in case it is lost be an assignment by another user (currently I am the only one using this but did it just to be sure it had the correct value.) Then I open the report again but still get the errors. I did not expect this at all. thought starting the report from scratch would certainly work. I even closed the form just after assigning strVar1.
then in final effort. When I close the form I run the exact same code:
strVar1 = InputBox("Enter Number of Weeks for report")
If strVar1 = "" Then Exit Sub
DoCmd.OpenReport "rptEquipPmSchedule", acViewReport
Which will force the user to input the value for strVal1. Even though this is not what I want but tried this for troubleshooting and I still get #ERROR.
When I run the report for a form that does not have any of the same field, no issues. When I run the report or keep it open with a requery from the form that has the same data, the report will not give the correct results. Note that if I run the query itself, the data in the query is correct.
i also tried using a number instead of Val1() in the query and got the same results.
i also tried the refresh button in the ribbon and get Unknown Function Name and all the data in the report is lost.
Anyone got any ideas??
While your textual explanation is difficult to understand the entire scope, consider re-assessing your workflow. The entire objective is to allow users to run customized criteria for reporting. And your main issue is the strVal does not persist in memory so all references to it fails.
Consider the following points:
Have users set criteria on a dedicated unbound form with button click for report where that report instance is immutable for viewing/printing only and if needed to be changed must be re-run (i.e., button re-clicked).
Access has no need for VBA's InputBox() as strVal can be an unbound textbox on this unbound form whose value remains intact for all open windows.
Have function and all its references point to form field: Form!myFormName!strValuetextbox
Because reports on pretty much any software/web app system is not used as a GUI interface to run actions, users will know if they intend to change report criteria, close current report or go back to entry point and change strVal then re-click button to re-run report.
Keep data entry/input (primary use of forms) separate from data export/output (like reports). From developer and user standpoint this compartmentalization will save you headaches down the road.

How to allow a combobox to be left blank//skipped on a form

I am relatively new to Access and any SQL and VBA I have picked up is self-taught, so I would really appreciate answers that are not too heavily laden with technical terms...that said, I am having an issue with allowing comboboxes to be left blank on a form if the user chooses not to input data. Also, I am using Access 2016.
Initially the problem I ran into was that if a combobox was entirely skipped and left blank, or if the user selects the combobox and then tries to move on without making a selection from the list, they got the error "You tried to assign the Null value to a variable that is not a Variant data type," and were unable to move on to any other fields on the form or to save the record being entered.
I found this article that details a possible solution, but I can't seem to get it to work. I made an unbound combobox, set the Row Source to:
SELECT EmailID, PersonalEmail FROM EmailT UNION SELECT "<N/A>","<N/A>" FROM EmailT
ORDER BY PersonalEmail;
where PersonalEmail is a field of type short text and the EmailID is an autonumber. I also followed the article's steps for formatting the combobox (column width, etc.) and set it to Limit to List = Yes.
Here is my exact code:
Private Sub Combo62_AfterUpdate()
If Combo62 = "<N/A>" Then
EmailID = Null
Else
EmailID = Combo62
End If
End Sub
Private Sub Form_Current()
If IsNull(EmailID) Then
Combo62 = "<N/A>"
Else
Combo62 = EmailID
End If
End Sub
< N/A> now shows up on my list, but if it is selected I get the error: "The value you entered isn't valid for this field. For example, you may have entered text in a numeric field or a number that is larger than the FieldSize setting permits."
Access's debugger highlights the line:
EmailID = Null
but I am not sure what steps I should take now to try and fix this.
I am completely open to other methods of allowing the combobox to be left blank if someone knows of a better way to do this also. For all I know, I am missing something really obvious! Thanks in advance for any insight!
EDIT: Thanks for your help guys, I never did figure out what exactly the problem was, but I got some advice from another forum to rethink my database design so this ended up being a null issue--it's all totally different now!
If the recordsource for your form contains a query with one-to-many relationships this error may come up. Try to use only the base table as the recordsource for the form. Use subforms if you need to display related data. The rest of the code may then be unnecessary.

How to open a form at a specific record in Access

I have a form with records for individual people with a button to view/edit a persons clearances. When I finish editing the clearance and press the Back button I want the original form (Basic Personal Information) to open at the record I have just been working on, rather than going back to record 1.
The code I have at present is
DoCmd.Close
DoCmd.OpenForm ("Basic Personal Information")
I have tried changing the OpenForm to
DoCmd.OpenForm ("Basic Personal Information"), , , "S_ID=LinkRef"
Where S_ID is the name of the field which has the persons unique ID and LinkRef is an integer of that ID. I tried a few small variations of this and eventually got it to sort of work, but it opened Basic Personal Information with only that one record available, so I couldn't look at any other people on the form (i.e. in the bottom left record navigation it was 1 of 1, when it should be for example 5 of 32).
Another thing I tried was adding the line
DoCmd.GoToRecord(acDataForm,"Basic Personal Information",acGoTo,"[S_ID] = " & LinkRef)
But obviously, the problem here is that the Offset for AcGoTo should just be a record number, so it should in this case be 5. But I don't know how to tell the program to figure out what number the record will be from the LinkRef.
I hope that makes sense, if not feel free to ask me questions and I will try to explain better, otherwise any suggestions/methods will be appreciated.
Thanks
I would go about your problem this way :
DoCmd.OpenForm ("Basic Personal Information")
Forms("Basic Personal Information").Form.Recordset.FindFirst "[S_ID] = " & _
Forms("PreviousForm").[LinkRef] & ""
Meaning, I would first open the form, and then move the recordset's cursor to the desired row.
maybe this help you: try to open form in dialog:
DoCmd.OpenForm ("Basic Personal Information"), , , , , acDialog
See this page: http://msdn.microsoft.com/en-us/library/bb237964(v=office.12).aspx
I was hoping to find some kind of indexOf() method, but I don't know what the containing class is called. So, here's a work around instead, because I'm not all that familiar with VB:
You can use GoToRecord to go to the first record by passing in 0 for the fourth parameter. Then, loop through all the records using acNext, until you find the entry with the correct S_ID value. Then you can stop there and your entry will be the current one in the list.
It's far from perfect or optimal but it'll work if no other options are presented