Probably a simple questions for you experts

NavyJoe

Board Regular
Joined
Sep 14, 2004
Messages
63
1. I created a form that contains award information. At the bottom I want to have a button that prints a report of that record only. I know how to get the print function, but my issue is how to get a report to "see" the active record in the form. Hope ya'll can help.

2. What is the IIF Switch and Choose Function? I saw it online looking for any VBA Instructions. Does it work like the IF Function in Excel?

3. Is there anywhere on site I can print a free VBA Code Manual?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Joe
  1. I think what you need to do is pass a parameter to the report from the form which uniquely identifies the current record.
  2. The Iif and Choose function are similar to their Excel equivalents Switch isn't available (ASFAIK) in Excel,
    to get a full explanation of how it works I would check the help files.
  3. Not that I know of, I actually don't find the standard help that bad, especially if you use the Index/Search tabs rather than
    the Contents one.
 
Upvote 0
Here's an intermediate technique idea for you.

Create a form based on a table or query.
Put the form into "Continuous Form" mode (this is the one that doesn't look like a 'table') and shows many records, just one at a time.

When in design mode, all you'll see are the given field names which you can move around however you wish. Add a button to that form with a 'click event'.

Now, here's the trick. When you open the form in regular mode (the nice one you see when you're using the form), each record will show up with it's own button. The act of clicking a button does two things. First, the record next to the button is "selected" AND the onclick event of the button is executed.

In the onclick event put code that references values in your table/query.
You can reference fields within the record using syntax such as:

Me.fieldname1

Now, back to Nories suggestion. Norie is suggesting you find some way to unique identify each record in your table. This is critically important because what it does is allow you to dynamically generate a query with a Parameter passed to it that will only affect your one selected record.

Assuming the field 'fieldname1' has that value, you can do something like this.

Code:
If Len(Me.fieldname1) > 0 Then
  Call BeginTransaction(Me.fieldname1)
End If

This does two things. First, it has a test to ensure something is in the identifying field. Next, it sends that information over to a function called 'BeginTransaction' which needs information passed to it (the value of fieldname1)

Code:
Sub BeginTransaction(ByRef strParameter As String)
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim qdf As QueryDef
Dim strSQL, strQryName As String

Set dbs = CurrentDb()

strSQL = "SELECT * FROM tblName "
strSQL = strSQL & " WHERE afieldname='" & strParameter & "'"
strQryName = "YourQuery"
Set qdf = dbs.CreateQueryDef(strQryName, strSQL)

End Sub

There's a few things to change here, this is just a sample that will create a select query for you. It will, of course, generate errors if you attempt to create a 2nd copy until you delete the first.

Now, here's how you do this for your particular problem.
Remove the qdf references above (they're not needed for this)
Below, .fields(0).value is equivalent to rs.fields(0).value and extracts the value in the very first field in the recordset (it starts at 0 by default)

I'll let you puzzle out what the rest of this does. But, as a hint, it allows you to control layout of the print (portrait vs landscape -- necessary since there is a documented bug where Access resets to Portrait when you close the database) and to view vs Print the report. As a note, make sure you have references set to the DAO 3.x object library AND whenever you add type declarations to a code module (2nd code segment below) you must add them to the top of the code module.

Code:
Set rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
With rs
  myOrient = 1       ' Landscape format    0 is Portrait
   testvalue = .fields(0).value
   Call OpenReport(testvalue, myOrient)
End with

Code:
Type str_DEVMODE
   RGB As String * 94
End Type

Type type_DEVMODE
   strDeviceName As String * 16
   intSpecVersion As Integer
   intDriverVersion As Integer
   intSize As Integer
   intDriverExtra As Integer
   lngFields As Long
   intOrientation As Integer
   intPaperSize As Integer
   intPaperLength As Integer
   intPaperWidth As Integer
   intScale As Integer
   intCopies As Integer
   intDefaultSource As Integer
   intPrintQuality As Integer
   intColor As Integer
   intDuplex As Integer
   intResolution As Integer
   intTTOption As Integer
   intCollate As Integer
   strFormName As String * 16
   lngPad As Long
   lngBits As Long
   lngPW As Long
   lngPH As Long
   lngDFI As Long
   lngDFr As Long
End Type
      
Public Function ConvertToLandscape(ByVal strName As Variant, myOrient As Variant)
   Dim rpt As Report
   Dim strDevModeExtra As String
   Dim DevString As str_DEVMODE
   Dim DM As type_DEVMODE
   
   'strName = "repTimeDelayedTest"
   DoCmd.OpenReport strName, acDesign 'Opens report in Design view.


Set rpt = Reports(strName)

If Not IsNull(rpt.PrtDevMode) Then
   strDevModeExtra = rpt.PrtDevMode
   DevString.RGB = strDevModeExtra
   LSet DM = DevString
   DM.lngFields = DM.lngFields Or DM.intOrientation 'Initialize fields.
   DM.intOrientation = myOrient 'Landscape
   LSet DevString = DM 'Update property.
   Mid(strDevModeExtra, 1, 94) = DevString.RGB
   rpt.PrtDevMode = strDevModeExtra
   DoCmd.Save acReport, strName
   DoCmd.Close acReport, strName
End If

End Function
Public Function OpenReport(ByVal repName As Variant, ByVal myOrient As Variant, _
                        Optional bolPrintOnly As Boolean)

Dim viewPref As VbMsgBoxResult
   
    If Not bolPrintOnly Then
        viewPref = MsgBox("Select Yes if you wish to print.", vbYesNo + vbDefaultButton2, "View or Print")
    End If
    
    Call ConvertToLandscape(repName, myOrient)
    If bolPrintOnly Then viewPref = 6
    Select Case viewPref
        Case 6  'Yes
            DoCmd.OpenReport repName, acNormal, "", ""
            DoCmd.Close acReport, repName
        Case Else  'No
            DoCmd.OpenReport repName, acPreview, "", ""
    End Select

End Function

Ps - Maybe I lied. This might be advanced technique, not intermediate.

Mike
 
Upvote 0
Another add on question, I thought I could figure this out, but I can't.

When the button is clicked (the form name where the button is located is Awards Log), I want the following to happen (keep in mind, the Report I want is connected to the SSS Form)

Form:SSS Field: Name = Form: Awards Log Field: First Name & " " & MI & "." & " " & Last Name

Theres like 8 other fields I want to automatically update to the value of what's in the Form: Award Log.

Last question. One of the fields in the SSS Form is Long Award. I want (in excel language)...once again when the button is pushed.

Form: SSS Field: Long Award = If (Form: Award Log/Award="JSAM", "Joint Staff Achievement Medal, If(...="JSCM","Joint Staff Commendation Medal, .....) there's like 6 awards. How do I get that into access language?
 
Upvote 0
Joe

When you say you want to print a report do you mean an actual report or the form?

I assumed you were referrring to an existing report that was based on the same data as your form.
 
Upvote 0
Norie,
That is correct, it's a Report that is based on an (hopefully) Automated Form that is based on a User Data Entered Form.


As for the report thing to get it to look at the active record.

Should I make 1 table. Put all my fields for all the forms in that 1 table. Then make a query that looks for the highest Record Number (autonumber). Then base the report off that and bring in the fields I want off the table. Would that make it easier.

Thanks
Joe
 
Upvote 0
Joe

First thing is to create the report which has a page for each record. To that add a group header for the record number and set it's Force New Page property appropriately.

Once you've got that working to your satisfaction you can introduce a parameter which takes the value from the form.

It sounds quite complicated I know and is hard to explain but is definitely possible. If you want I could Email you a small zipped example - PM me if you do.
 
Upvote 0

Forum statistics

Threads
1,221,834
Messages
6,162,268
Members
451,758
Latest member
lmcquade91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top