Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Consolidate Code from Two Command Button into One

Status
Not open for further replies.

LadyDev

Programmer
Jan 29, 2003
86
US
Can command buttons perform two functions at once. I have two command buttons. One opens a text box and the other pulls information from a table and displays it to a list box. Both buttons pertain to the same thing. The text box is giving a description of the information being displayed in the list box.

Can I pulled both those functions into just one coomand button -- with the text box displaying first and then the other information last? How?

Thanks!
 
Yes, you can do this.

Show me the code you are using on each command button and which you want to come first.

Judge Hopkins

"...like hunting skunks underwater...."
John Steinbeck, The Grapes of Wrath
 
This button when selected fills and displays the listbox information.

Private Sub lstMain_Click()

DoCmd.SetWarnings False

Select Case Me![lstMain].ListIndex
Case 0 'ACBD10
Forms![frmMain]!lstSubCategory.RowSourceType = "Table/Query"
Forms![frmMain]!lstSubCategory.RowSource = "SELECT * FROM [tblMenuItems] Where [MenuName]='ACBD10' Order by [SortOrder]"
Forms![frmMain]!lstSubCategory.Requery

This button opens form that gives a description of the files in the listbox. I want this command to kick-off first.

Private Sub Command32_Click()

' Open form to show description about the list
' in the current record.

Dim strDocName As String
Dim strLinkCriteria As String

On Error GoTo ErrorHandler

strDocName = "Popup"
strLinkCriteria = "SortOrder"
DoCmd.OpenForm strDocName, , , strLinkCriteria

Forms!frmDemoTemplate.SetFocus 'Set focus back to form

Exit Sub

ErrorHandler:
MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description

End Sub

Thanks!
 
Save your database as test.mdb so you won't mess up anything.

On the command button, put all of the code, starting with:

Private Sub Command32_Click()
...
End Sub

and then add below it:

Private Sub lstMain_Click()
...
End Sub

When you click the command button, the first procedure will run and when it is through, the second should run.

Judge Hopkins

"...like hunting skunks underwater...."
John Steinbeck, The Grapes of Wrath
 
Sorry that bombs....how could that work if I am referencing the same button with two different names?
 
I was not clear and I apologize.

Let me try again:

Save your database as test.mdb so you won't mess up anything.

On the command button, put all of the code, starting with:

Private Sub Command32_Click()
(and, obviously, delete the End Sub)
and then add below it:

all of the code from Private Sub lstMain_Click()
(but, of course, do not use that procedure name; the code from this procedure will be part of Command32_Click())
...
(and, obviously, add the End Sub)

When you click the command button, the first procedure will run and when it is through, the second should run.

Judge Hopkins

"...like hunting skunks underwater...."
John Steinbeck, The Grapes of Wrath
 
Private Sub Command32_Click()

' Open form to show description about the list
' in the current record.

Dim strDocName As String
Dim strLinkCriteria As String

On Error GoTo ErrorHandler

strDocName = "Popup"
strLinkCriteria = "SortOrder"
DoCmd.OpenForm strDocName, , , strLinkCriteria

Forms!frmDemoTemplate.SetFocus 'Set focus back to form

Exit Sub

ErrorHandler:
MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description
DoCmd.SetWarnings False

Select Case Me![lstMain].ListIndex
Case 0 'ACBD10
Forms![frmMain]!lstSubCategory.RowSourceType = "Table/Query"
Forms![frmMain]!lstSubCategory.RowSource = "SELECT * FROM [tblMenuItems] Where [MenuName]='ACBD10' Order by [SortOrder]"
Forms![frmMain]!lstSubCategory.Requery

End Sub


Judge Hopkins

"...like hunting skunks underwater...."
John Steinbeck, The Grapes of Wrath
 
Yes and No. It has the illusion of working. The popup pops up, but the listview information is not visible.

If I click in the listbox, information does appear in the second listview, but nothing in the first. (the listview is setup that if you select a file in the first the second displays the list files).
 
Let me try something else; here is the code I use for an options group. Depending on which one is selected, it pulls up a form; in this case, a delete query. I am not really sure, after re-reading your questions, exactly what it is you want to do...but it is Friday of a hard week so do not blame yourself!

I think what I am doing is backwards of what you are trying to do. Anyway, here is the code to open forms and the code from just one form.



Private Sub FrameDeleteOptions_AfterUpdate()
'this is the delete options group
Dim strFormName As String
'form name dimensioned as string

Select Case Me.FrameDeleteOptions
'select case for what happens when a button is pushed
Case 1: strFormName = "frmDeleteLender"
Case 2: strFormName = "frmDeleteCity"
Case 3: strFormName = "frmDeleteCounty"
Case 4: strFormName = "frmDeleteZIPDiskOwners"
Case 5: strFormName = "frmDeleteRealEstateCompanies"
End Select


Option Compare Database
Option Explicit
'Delete a lender
'
' Notes:
' ---
' The code created was in response to a newsgroup posting
' The underscore character used in the code is a line-continuation character
'

Private Sub cmdCancel_Click()

On Error GoTo Proc_Err

DoCmd.Close ObjectType:=acForm, ObjectName:=Me.Name

Proc_Exit:
Exit Sub

Proc_Err:
MsgBox Prompt:=Err.Description, Buttons:=vbExclamation
Resume Proc_Exit

End Sub

Private Sub cmdOK_Click()

On Error GoTo Proc_Err

If IsNull(Me.cboLenders) Then

' The user clicked OK without selecting an entry
MsgBox _
Prompt:="A selection must be made before proceeding.", _
Buttons:=vbInformation + vbOKOnly, _
Title:="Choose a lender to PERMANENTLY delete!"
Me.cboLenders.SetFocus

Else

' The user made a selection. Display a "second chance"
' prompt to be sure the user wants to delete the entry
'
If vbYes = MsgBox _
( _
Prompt:="You have selected " & Me.cboLenders & _
" to be deleted." & vbCrLf & "Are you sure?", _
Buttons:=vbExclamation + vbYesNo + vbDefaultButton2, _
Title:="This lender will be PERMANENTLY deleted!" _
) Then

' The user wants to delete the entry
If DeleteEntry(Me.cboLenders) Then
MsgBox _
Prompt:="The selected lender was successfuly deleted.", _
Buttons:=vbInformation + vbOKOnly, _
Title:="This lender: " & Me.cboLenders & _
" was PERMANENTLY deleted!" _

' Now that the entry was deleted, requery the combo box so that
' the entry is removed from the list. Otherwise, Access gives a
' weird result in the place where the lender existed.
'
Me.cboLenders.Requery

' Clear the combo box so another entry can be selected.
Me.cboLenders = Null

Else
MsgBox _
Prompt:="The selected lender could not be deleted.", _
Buttons:=vbExclamation + vbOKOnly
End If

End If

End If

Proc_Exit:
Exit Sub

Proc_Err:
MsgBox Prompt:=Err.Description, Buttons:=vbExclamation
Resume Proc_Exit

End Sub

Private Function DeleteEntry( _
pstrEntry As String) As Boolean

'
' Comments: This will delete the lender entry that was selected.
'
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim fRetVal As Boolean

On Error GoTo Proc_Err

' This is how you execute a query programmatically and how
' you pass it a parameter.
'
Set dbs = CurrentDb()
Set qdf = dbs.QueryDefs("qryDeleteLenderEntry")
qdf![prmLender] = pstrEntry
qdf.Execute

' Return a TRUE value to indicate the process completed successfully
'
fRetVal = True

Proc_Exit:
Set qdf = Nothing
Set dbs = Nothing
DeleteEntry = fRetVal
Exit Function

Proc_Err:
fRetVal = False
MsgBox Prompt:=Err.Description, Buttons:=vbExclamation
Resume Proc_Exit

End Function


Judge Hopkins

"...like hunting skunks underwater...."
John Steinbeck, The Grapes of Wrath
 
I am trying to get one command button to perform two functions consecutively (1) open a form (2) display information from a table, based on selection, in a listview. This data is in picture format (.gif, .bmp. jpg, etc)

But I am now trying something different, because I have been unsuccessful at getting this to work.

I came across the Explorer, published on Access Web, that allows you to display your directory files. I thought I could use that for this project. However, as written it displays every directory on the system. Do you happen to know where I would start to modify this module just to display files from 'C' dirve? Is it something simple that I may be overlooking?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top