Display of data in access report based on form selection - ms-access-2007

I have a check box on my access form. If the user selects that check box , one column in an access report should display the values in 1000's
How can this be done?

By passing the value of the checkbox into the OpenArgs when you run the report, you can set the format of the column. Here's what the report opening code might look like:
Private Sub cmdRun_Click()
Dim bFormatted As Boolean
bFormatted = chkFormat.Value
DoCmd.OpenReport "ReportName", acViewPreview, , , , bFormatted
End Sub
Then in the Open event of the Report:
Private Sub Report_Open(Cancel As Integer)
Dim sArgs As String
Dim bFormatted As Boolean
sArgs = OpenArgs & ""
If sArgs <> "" Then
bFormatted = CBool(sArgs)
End If
If bFormatted Then
txtBox.Format = "0000"
End If
End Sub
I have not checked the correct format. That's left up to you.

Related

Textbox on Access form passing previous value

I created a search form.
There is a text box for the search text. The user triggers the search event by either clicking on the search button or pressing the enter key.
The button click event is working.
On pressing of enter key for the first time the text box is passing the value which was entered in the text box from the previous event. If I press the enter key a second time the current value is passed.
For instance on the form load the text box is empty then I enter the search text as "Ali" and press enter key then the value "NULL" is passed and once again I press enter key then the value "Ali" is passed.
Option Compare Database
Option Explicit
Private Sub btnSearch_Click()
Dim SQL, strSearch As String
strSearch = Nz(Me.txtSearch.Value, "")
SQL = "SELECT tbl_mst_Employee_Details.emp_ID, " _
& "tbl_mst_Employee_Details.emp_Name " _
& "FROM tbl_mst_Employee_Details " _
& "where [emp_Name] like '*" & strSearch & "*' " _
& "ORDER BY tbl_mst_Employee_Details.emp_Name;"
Me.lstEmployee.RowSource = SQL
Me.lstEmployee.Requery
Me.txtEmpID.Value = ""
Me.txtEmpName.Value = ""
Me.Refresh
End Sub
Private Sub Form_Load()
Me.txtEmpID.Value = ""
Me.txtEmpName.Value = ""
Me.txtSearch.Value = ""
End Sub
Private Sub lstEmployee_Click()
With lstEmployee
Me.txtEmpID.Value = .Column(0)
Me.txtEmpName.Value = .Column(1)
End With
End Sub
Private Sub lstEmployee_DblClick(Cancel As Integer)
Forms!frmEmployeeShiftDetails.txtEmpID.Value = Me.txtEmpID
Forms!frmEmployeeShiftDetails.txtEmpName.Value = Me.txtEmpName
DoCmd.Close
Forms!frmEmployeeShiftDetails.txtEmpID.SetFocus
End Sub
Private Sub lstEmployee_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
lstEmployee_DblClick (0)
End If
End Sub
Private Sub txtSearch_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
btnSearch_Click
End If
End Sub
Simply set the focus to the search button first.
Additionally I would also check for pressing tab (and use constants for both, return and tab).
For having constant UI behaviour I would take care that the search button receives the focus when pressing tab in the search box, so set the correct tab order in the form.
Private Sub txtSearch_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
btnSearch.SetFocus
btnSearch_Click
End If
End Sub

Opening Form From Report

I have the following lines of code in 2 different VBA functions in a Module they both have the same aim of opening a form to a specific record;
stLinkCriteria = "[ID]=" & Reports![Rpt_Manufacture].[ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
and
stLinkCriteria = "[ID]=" & Forms![frmManufactureList]![frm_Products].[ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
How can I change this so I only have one function that I can call from reports or forms and it will open the form to a specific record. I have tried the me! version on reports but I get an 'Invalid use of Me keyword' which I guess is because I can not use it from a Module.
UPDATE #1
Based on the answer below by Thomas G I used this code;
Option Compare Database
Public Function CmdOpenProductDetails(ByRef theObject As Object)
On Error GoTo Err_CmdOpenProductDetails
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "FrmProductDetails"
stLinkCriteria = "[ProductID]=" & theObject![ProductID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_CmdOpenProductDetails:
Exit Function
Err_CmdOpenProductDetails:
MsgBox Err.Description
Resume Exit_CmdOpenProductDetails
End Function
And from the Form and Report for the product names I have an onClick event that reads;
=CmdOpenProductDetails()
However, if I click a product name on a form I get the error message;
The express On Click you entered as the event property setting
produced the following error. The expression you entered has a
function contain the wrong number of arguments. (The expression may
not result in the name of a macro, UDF or Event Proc) (There may have
been an error evaluating the function).
If I click from a report I get the error message;
MS Access cannot find the object 'CmdOpenProductDetails(). Make sure
you have saved it and that you have typed it correctly.
Pass the form byref in a sub
SOmething like
Public Sub Open_Form(ByRef theForm As Form)
Dim stLinkCriteria As String
stLinkCriteria = "[ID]=" & theForm![ID]
DoCmd.OpenForm theForm, , , stLinkCriteria
End Sub
Private Sub TestIt()
Open_Form Forms![frmManufactureList]![frm_Products]
End Sub
Note that you might have to tweak it a bit because I dont know the exact context. Maybe you have a subform and so you should pass the mainform instead
But the idea is there
UPDATE following Erik proposal:
You can pass either a form or a report as an object to make it work for both
Public Sub Open_Form_or_Report(ByRef theObject As Object)
Dim stLinkCriteria As String
stLinkCriteria = "[ID]=" & theObject![ID]
If TypeOf theObject Is Form Then
DoCmd.OpenForm theObject , , , stLinkCriteria
ElseIf TypeOf theObject Is Report Then
DoCmd.OpenReport theObject , , , stLinkCriteria
Else
MsgBox "Error : the type should be a Form or a Report"
End
End Sub
Private Sub TestIt()
Open_Form_or_Report Forms![frmManufactureList]![frm_Products]
End Sub

Get ID Of Selected Row In Sub Form

I have a Parent Form with a Sub Form and I want the user to be able to select a record from the sub form, then click a button on the Parent Form, which will launch a "new" form that has full demo's pertaining to the selected record from the sub form.
How would I do this in Access 2013?
You can pass the ID as a parameter when opening the "new" form.
On your button's Click event:
Private Sub Command0_Click()
'Get the ID
Dim id_ As Long
id_ = Me.SubformName.Form!ID
'Open the new form and pass the ID to the .OpenArgs
DoCmd.OpenForm "FormName", acNormal, , , acFormPropertySettings, acWindowNormal, id_
End Sub
On the Form's Load event, check the .OpenArgs and filter the form (or whatever else you need to do) to the supplied ID.
Private Sub Form_Load()
With Me
If Not IsNull(.OpenArgs) Then
.Filter = "[ID]=" & .OpenArgs
.FilterOn = True
.Caption = "ID: " & .OpenArgs
End If
End With
End Sub

SubForm with parameters from the main form

I have a form with a subform that are linked by an ID. The problem is the subform has a combo box that needs a parameter from the main form for the row source.
The form is about sales calls and they need to be able to link many proposals and contracts as they want. The combo box for the list of proposals and contracts needs to be in relation with the mills or clients of this sales call.
I know I could do a Forms!FormName!ControlName to get the mill list but does this mean I have a design problem?
Or should I do a list filled when a user click on an element in a combo box in the main form? but then I would have to handle the save and delete myself.
Thank you
If a subform requires a main form to function properly you can check for a parent. For example:
Private Sub Form_Open(Cancel As Integer)
Dim strParent As String
Dim strSubname As String
GetParent Me, strParent, strSubname
If strParent = "None" Then
MsgBox "This is not a stand-alone form.", , "Form Open Error"
Cancel = True
End If
End Sub
Function GetParent(frm, ByRef strParent, ByRef strSubname)
Dim ctl
On Error Resume Next
strParent = frm.Parent.Name
If Err.Number = 2452 Then
Err.Clear
strParent = "None"
Else
For Each ctl In frm.Parent.Controls
If ctl.ControlType = acSubform Then
If ctl.SourceObject = frm.Name Then
strSubname = ctl.Name
End If
End If
Next
End If
End Function

Adding description into TextBoxes within an Excel VBA form that dissapears once user starts typing inside it

We have a userform with multiple textboxes and we would like to build something similar to the link image below, in terms of showing what the user should input in each text box:
http://d2o0t5hpnwv4c1.cloudfront.net/426_formsBestPractices/comments.png
The "default" text would disappear once the user starts typing (as opposed than once the user "lands" cursor within the textbox.
Also, if nothing gets entered within the textbox the default text would not be submitted and a blank would be used.
Can this be done?
Any suggestions will be greatly appreciated.
Can I ask why you want the default text to dissapear once a user changes the text and not once they enter the textbox?
This is not what most users will expect, I think it will be slightly confusing for some and wouldn't recommend it. The user will most likely try and delete the old text before typing their new text creating extra work.
I would use something like this:
Const sNameDefault As String = "Your Name Here"
Const sEmailDefault As String = "Your Email Here"
Private Sub UserForm_Initialize()
Me.TextBox1.Text = sNameDefault
Me.TextBox2.Text = sEmailDefault
CommandButton1.SetFocus
End Sub
'// TextBox1 - Name
Private Sub TextBox1_Enter()
With Me.TextBox1
If .Text = sNameDefault Then .Text = vbNullString
End With
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox1
If .Text = vbNullString Then .Text = sNameDefault
End With
End Sub
'// TextBox2 - Email
Private Sub TextBox2_Enter()
With Me.TextBox2
If .Text = sEmailDefault Then .Text = vbNullString
End With
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox2
If .Text = vbNullString Then .Text = sEmailDefault
End With
End Sub
Private Sub CommandButton1_Click()
Dim sName As String, sEmail As String
'// Get Name
If Me.TextBox1.Text = sNameDefault Then
sName = vbNullString
Else
sName = Me.TextBox1.Text
End If
'// Get Email
If Me.TextBox2.Text = sEmailDefault Then
sEmail = vbNullString
Else
sEmail = Me.TextBox2.Text
End If
MsgBox ("Your Name: " & sName & vbNewLine & " Your Email:" & sEmail)
Unload Me
End Sub
The above example is simply a userform with two textbox's and a commandbutton. Clicking inside the textbox will clear the default text. If the user enters nothing clicking another textbox or control will cause the default text to be added back. Once the command button is clicked the code will return blank if the default text remains.
Yes it is possible :)
I have created a sample for you. You can download it from here.
http://wikisend.com/download/143478/Sample.xlsm
The trick is to create 2 similar TextBoxes and hide the 'original' one behind the dummy TextBox ("Which has the default text")
When you start typing in the dummy, the text will actually be typed in the textbox which is hidden.
And when you are pulling values, simply pull the values from the 2nd text box so the default data is not considered :)
Hope this helps.
Code Used
Private Sub UserForm_Initialize()
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End Sub
Private Sub CommandButton1_Click()
MsgBox TextBox2.Text
End Sub
Private Sub TextBox1_Change()
TextBox1.Visible = False
With TextBox2
.Text = Replace(TextBox1.Text, "Please enter your name", "")
.Visible = True
.SetFocus
.SelStart = Len(TextBox2.Text)
End With
End Sub