INSERT INTO table in access from inputbox - vba

I'm stumped - and also very new to VBA in access but here is my intent:
I have want the member to select a "Begin" button in access, from there, multiple inputboxes will pop up allowing the user to enter data. I want the data entered (name, date, etc.) to be stored into a label on the form, and at the end the next person who opens the "begin" button has a fresh bunch of inputboxes pop-up and their data is then stored in each label of form. End game, a pretty form is emailed to me.
I've mastered the inputboxes and msgboxes - they pop up and run all the way through. The problem I'm at is when the user enters their information I need it to go onto the form into the appropriate fields so in the end the form can be "printed" as a .pdf and sent to me. Any help would be greatly appreciated!
Sub Begin()
Dim AddName As String
Dim AddAddress As String
Dim AddSupv As String
Dim AddPhone As String
AddName = InputBox("What is your rank and full name?", "Enter Rank and Name")
AddAddress = InputBox("What is your home address? Please include street, city, state and zip.", "Enter Address")
Where is any, would I add a function to insert the data after each inputbox into a field on my form. Form Name is Form1 and I also have a table labeled 'AUS Table' that have fields called 'Name_and_Rank' and 'Address' if the inputbox data must go to a table first.

Variables are not needed, just reference controls.
For label controls:
Me.lblName.Caption = InputBox("What is your rank and full name?", "Enter Rank and Name")
Me.lblAddress.Caption = InputBox("What is your home address? Please include street, city, state and zip.", "Enter Address")
For textbox controls:
Me.tbxName = InputBox("What is your rank and full name?", "Enter Rank and Name")
Me.tbxAddress = InputBox("What is your home address? Please include street, city, state and zip.", "Enter Address")
But if there are textboxes on form, users should just type directly into textboxes.

Related

How to ensure selected combobox item is in table? (Access VBA)

My goal is to create a way to log why something was changed and who changed it.
I have 2 comboboxes that are populated by tables. I use VBA to define the RowSource when the form opens.
When the user clicks the send button, before anything is changed, I want to make sure that the comboboxes have 'valid' selections.
One combobox is a list of employees based on firstname & ' ' & lastname
the other is a list of pre-set reasons (i.e. "Incorrect Hours", Wrong Item #", etc.)
If Nz(Me.cmbReason, "") = "" Then
fncMsgBox "Must select a reason from the dropdown for modifying a labor entry."
GoTo Exit_btnSend_Click
Else
If Not IsNull(DLookup("Reason", "tblEditReason", Me.cmbReason)) Then
fncMsgBox "Must select a reason from the dropdown for modifying a labor entry."
GoTo Exit_btnSend_Click
End If
End If
If Nz(Me.cmbModBy, "") = "" Then
fncMsgBox "Must enter who is modifying this labor entry."
GoTo Exit_btnSend_Click
Else
If IsNull(DLookup("FNAME & ' ' & LNAME", "EMP", Me.cmbModBy)) Then
fncMsgBox Me.cmbModBy & " is not a valid name."
GoTo Exit_btnSend_Click
End If
End If
When I have "If not IsNull..." then it won't accept anything regardless of valid or invalid, if I remove the "IsNull" then I get a mismatch debug.
How do I ensure that what the user puts inside the combo box, is in fact in the table?
June7 Comment - Maybe set combobox LimitToList property to Yes?
Resolved my problem.
LimitToList prevents anything not in the list from being accepted. When clicking on any other control in the form, it pops out the drop down until an item from the list is selected or it is left blank. Allowing the code I was using "If not IsNull(....." to do just what I was looking for!

Creating a form in Access to get the Customer Information

I have created a database named Customer_master in which I have some customer information saved in an MS Access database.
I am trying to create a Form so that I can enter a Mobile Number and click the Search button and get the Customer Name.
The form is created with the Mobile Number & Customer Name as text boxes and a Search button in front of the mobile number.
As I am new to VBA, I think there is some coding issue.
Below is the code I have tried for the Search button:
Private Sub Command6_Click()
Dim strsearch As String
Dim Task As String
'Check if a keyword entered or not
If IsNull(Me.Mobile_Number) Or Me.Mobile_Number = "" Then
MsgBox "Please type in your search keyword.", vbOKOnly, "Keyword Needed"
Else
strsearch = Me.Mobile_Number.Value
Task = "SELECT Customer_Name FROM Customer_Master WHERE ((Mobile_Number Like ""*" & strsearch & "*""))"
Me.RecordSource = Task
End If
End Sub
After I enter the Mobile Number and click on the Search button nothing is happening as I am not getting the name of the customer.
An alternative way to achieve this is to create a new query in access against the datasource, and in the query definition pass the value from your search form to the query, using FORMS!VALUE.
You can then add a button to your form and save the VBA code to execute the query on click. by adding in the line DoCmd.OpenQuery

Add a Group E-mail command button to a Parameter Form in ms access 2010

I have an unbound parameter form based on a query called q_t_A.
Below you will see the code for the on click event of the command button cmdSendReport.
This attaches the report to a blank e-mail which allows me to choose the recipient.
I was wondering if I could add a cmdGroupSendReport button so that the report could be sent to a group of recipients.
In the underlying parameter form query q_t_A, I have a Yes/No field called EmailMailout, which singles out those who only want to have reports emailed to them.
I am planning on adding a checkbox parameter to the form chkEmailReportsOnly based on the Yes/No field EmailMailout so that if checked/true, only those email addresses will get the report sent to them by e-mail.
Is this the best way to do that? Secondly, how do I modify the code below so that All of the e-mail recipients receive the report shown in the combobox cboReports?
Private Sub cmdSendReport_Click()
If Not IsNull(cboReports) And cboReports <> "" Then
DoCmd.SendObject acSendReport, Me.cboReports, acFormatPDF
Elsed
MsgBox ("Please make a Report selection first from the drop down list to the left.")
cboReports.SetFocus
End If
cboReports = ""
End Sub
Thank you very much for your help...I am really new to this and so very thankful for your suggestions.
Oblio
Highlight 'SendObject' and click F1 to get Help. This object lets you add To, Cc, Bcc etc.
Loop through the records on your form, to create a string of email addresses to send To, eg:
Dim rs as recordset, s$
Set rs= Forms!chkEmailReportsOnly.RecordsetClone: With rs
While not .EOF
If !SendEmail then s = s & ";" & !Email
Wend
.close: End with
Set rs= nothing
s=mid(s,2)

Microsoft Access - Show name of logged in user from UserID

I have an Access database I'm working on which has a simple login system consisting of a drop down (combo) menu to select the username, a password field and a users table.
Inside the users table, I'm storing the username, password and the first name of all the users. I've managed to get my login form working, but I'd like the user's first name to be displayed in a text box on the next form.
I've managed to get the user's ID number, and display it in a text box, but I haven't been able to get the user's first name.
Does anyone know of a simple way to display the user's first name in a text box, keeping in mind I'll have several forms where I wish to display the user's first name?
Thanks
As your description says you are using a ComboBox, I am sure the RowSource would be.
SELECT ID, userNameField
FROM EmpAuth;
The ComboBox properties are Bound Column - 1, Column Count - 2, Column Widths - 0cm;2.5cm (something like this, but 0cm for sure). Now all you have to do is, make the following changes, RowSource :
SELECT ID, userNameField, firstNameField
FROM EmpAuth;
ComboBox properties are Bound Column - 1, Column Count - 3, Column Widths - 0cm;2.5cm**;0cm**
Then, you can simply use the OpenArgs method where you can pass the ComboBoxes Column 3. like,
If Me.password.Value = DLookup("password", "EmpAuth", _
"[ID]=" & Me.username.Value) Then
ID = Me.username.Value
DoCmd.OpenForm "POSMenu", OpenArgs:=Me.username.Column(2)
'Close logon form and open splash screen
DoCmd.Close acForm, "AuthenticationService", acSaveNo
Else
MsgBox.............
Then Finally in the Second Form's On Load event you can use.
Private Sub Form_Load()
If Len(Me.OpenArgs & vbNullString) > 0 Then _
Me.yourTextBoxName = Me.OpenArgs
End Sub
Hope this helps.
If you are going to use the user name information in several forms, perhaps you may consider using session variables to access the information. Its a bit more cleaner than passing it from object to object as an argument.
'On the login page,upon confirming the login is correct,
'Initialise session variables
TempVars.RemoveAll 'Destroy any previous Session
'Retrieve the username from the table
UserName = Dlookup("Usernamecol","UsersTable","UserID=" & SelectedUSerID
'Load the retrieved username into a global session variable
TempVars.Add "GlbUserName", UserName
'Use this whenever you want to show the user name
LoggedInUser = TempVars![GlbUserName]
Me.txtUserName = LoggedInUser
'or
Me.lblUserName.caption = LoggedInUser

Creating a dynamic table from a combobox in Access

I am trying to set up a query where a field is searched for on a table, the table I want to select the information from will change each time. I have tables for different years of admission to a club (2011,2012 etc). I want a user to be able to select a year from a combobox on a form and then this dynamically change the table the data is being selected from.
The code I am using makes sense to me, but I am a novice. Access says there is a syntax error. Please help!
Code:
SELECT [Admission No#]
FROM [Forms]![Control Form]![YearSelect];
it would be better to use a filter.
Create a form (Continuous Forms or Datasheet) to show search result. You can open it or use as a subform.
Implement OnClick event for Search button.
Dim stFilter as String
stFilter = "age = " & Me.FLD_AGE
'-- first way
DoCmd.OpenForm "SearchResult", , , stFilter
'-- second way
With Me.SearchResult.Form '-- SearchResult is the name of SubForm control
.Filter = stFilter
.FilterOn = True
End With