|
 |
access thread: Add New Record Button
Message #1 by "KennethMungwira" <KennethMungwira@Y...> on Mon, 17 Dec 2001 22:33:32
|
|
Due to the code in my form the AllowAddition.is not enabled. I would like
to only be able to insert a new record at the end of a perticular record
set. For e.g. I have many Records, but would like to study the records
three different ways. Each time I want to study a record another way I
would like to creat another study form.
1) How do I count the number of current records and only enable the new
record button on the last record.
2) Make it visible only on the last record.
(*Note the First record Created will always be visible*)
Attached is code behind the form:
Option Compare Database
Option Explicit
Private Sub FormPermits(bolOK As Boolean)
With Me
.AllowEdits = bolOK
'.AllowAdditions = bolOK
!StudySpec.Form.AllowEdits = bolOK
!StudySpec.Form.AllowAdditions = bolOK
!StudySpec.Form.AllowDeletions = bolOK
End With
Me!StudyID.SetFocus
' Make the cmdSave button visible
cmdSave.Visible = bolOK
End Sub
Private Sub cmdEditUndo_Click()
On Error GoTo Err_cmdEditUndo_Click
Dim ctl As Control
With cmdEditUndo
If .Caption = "Change This Record" Then
' Change the font color and caption
' of the cmdEditUndo button
.Caption = "Undo All Changes"
.ForeColor = vbRed
' Change the caption of the form Headers
lblHeader1.Caption = "Edit Study"
lblHeader2.Caption = "Edit Study"
' Allow editing of the main and sub forms
Call FormPermits(True)
' Copy the data from each combobox's and textbox's
' Control Source property to each control's
' tag property
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acComboBox, acTextBox
'MsgBox ctl.Name
If Not IsNull(ctl) Then
ctl.Tag = ctl
Else
ctl.Tag = ""
End If
End Select
Next ctl
' Do the same for the subform's fields
For Each ctl In Me!StudySpec.Form
Select Case ctl.ControlType
Case acComboBox, acTextBox
'MsgBox ctl.Name
If Not IsNull(ctl) Then
ctl.Tag = ctl
Else
ctl.Tag = ""
End If
End Select
Next ctl
Else
' Change the font color and caption
' of the cmdEditUndo button
.Caption = "Change This Record"
.ForeColor = vbBlue
' Change the caption of the form Headers
lblHeader1.Caption = "View Study "
lblHeader2.Caption = "View Study "
' Undo any changes by copying the data from
' the control's Tag property to the control's
' Control Source property
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acComboBox, acTextBox
'MsgBox ctl.Name
If Len(ctl.Tag) > 0 And ctl.Name <> "StudyID" Then
ctl = ctl.Tag
ctl.Tag = ""
ElseIf ctl.Name <> "StudyID" Then
ctl = " "
End If
End Select
Next ctl
' Do the same for the subform's fields
For Each ctl In Me!StudySpec.Form
Select Case ctl.ControlType
Case acComboBox, acTextBox
' MsgBox ctl.Name
If Len(ctl.Tag) > 0 Then
ctl = ctl.Tag
ctl.Tag = ""
ElseIf ctl.Name <> "duedate" Then
ctl = " "
End If
End Select
Next ctl
' Do not allow editing of the main and sub forms
Call cmdSave_Click
' Call FormPermits(False)
'MsgBox "else"
End If
End With
Exit_cmdEditUndo_Click:
Exit Sub
Err_cmdEditUndo_Click:
MsgBox Err.Description
Resume Exit_cmdEditUndo_Click
End Sub
'Private Sub Form_BeforeUpdate(Cancel As Integer)
'Me.txtNTID.Value = GetNTUserID
'End Sub
Private Sub Form_Current()
On Error GoTo Form_Current_ERR
' Change the cmdEditUndo caption and
' font color. Change the form's headers
' captions and do not permit editing of
' any records when we move to another
' record.
If Me.AllowDeletions = True Then
cmdEditUndo.Visible = False
lblHeader1.Caption = "Add Study Data "
lblHeader2.Caption = "Add Study Data "
ElseIf Me.AllowEdits = True Then
Call FormPermits(False)
With cmdEditUndo
.Caption = "Change This Record"
.ForeColor = vbBlue
End With
' Hide the cmdSave button if it is visible
cmdSave.Visible = False
lblHeader1.Caption = "View Study "
lblHeader2.Caption = "View Study "
Me.AllowEdits = False
End If
Form_Current_EXIT:
Exit Sub
Form_Current_ERR:
MsgBox Err.Description
Resume Form_Current_EXIT
End Sub
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
DoCmd.Close
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
MsgBox Err.Description
Resume Exit_cmdClose_Click
End Sub
Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
With cmdEditUndo
.Caption = "Change This Record"
.ForeColor = vbBlue
cmdEditUndo.Visible = True
End With
lblHeader1.Caption = "View Study"
lblHeader2.Caption = "View Study"
Call FormPermits(False)
cmdEditUndo.Visible = True
Exit_cmdSave_Click:
cmdSave.Visible = False
Exit Sub
Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click
End Sub
Private Sub Command44_Click()
On Error GoTo Err_Command44_Click
If Command44 = True Then
Me.AllowAdditions = True
Else: Me.AllowAdditions = False
DoCmd.GoToRecord , , acNewRec
End If
Exit_Command44_Click:
Exit Sub
Err_Command44_Click:
MsgBox Err.Description
Resume Exit_Command44_Click
End Sub
Message #2 by "David Chapman" <luckychap@b...> on Tue, 18 Dec 2001 19:20:16 +1030
|
|
In all of my projects I use different (similar) forms for browsing and
editing. Editing includes adding new records, and the forms for editing
usually include combo boxes. It helps to avoid unintentional editing, locks
records only when editing, and my users appreciate browse mode and edit
mode.
In a similar situation to what you have described I have used a subform at
the bottom of the screen to add a record, rather than have every record on
the continuous form available for editing. But there are thousands of cats
to kill and all of them are different.
David
----- Original Message -----
From: "KennethMungwira" <KennethMungwira@Y...>
To: "Access" <access@p...>
Sent: Monday, December 17, 2001 10:33 PM
Subject: [access] Add New Record Button
> Due to the code in my form the AllowAddition.is not enabled. I would like
> to only be able to insert a new record at the end of a perticular record
> set. For e.g. I have many Records, but would like to study the records
> three different ways. Each time I want to study a record another way I
> would like to creat another study form.
> 1) How do I count the number of current records and only enable the new
> record button on the last record.
> 2) Make it visible only on the last record.
> (*Note the First record Created will always be visible*)
>
> Attached is code behind the form:
>
> Option Compare Database
> Option Explicit
>
> Private Sub FormPermits(bolOK As Boolean)
>
> With Me
> .AllowEdits = bolOK
> '.AllowAdditions = bolOK
> !StudySpec.Form.AllowEdits = bolOK
> !StudySpec.Form.AllowAdditions = bolOK
> !StudySpec.Form.AllowDeletions = bolOK
> End With
> Me!StudyID.SetFocus
> ' Make the cmdSave button visible
> cmdSave.Visible = bolOK
>
>
> End Sub
>
> Private Sub cmdEditUndo_Click()
> On Error GoTo Err_cmdEditUndo_Click
>
> Dim ctl As Control
> With cmdEditUndo
> If .Caption = "Change This Record" Then
> ' Change the font color and caption
> ' of the cmdEditUndo button
> .Caption = "Undo All Changes"
> .ForeColor = vbRed
> ' Change the caption of the form Headers
> lblHeader1.Caption = "Edit Study"
> lblHeader2.Caption = "Edit Study"
> ' Allow editing of the main and sub forms
> Call FormPermits(True)
> ' Copy the data from each combobox's and textbox's
> ' Control Source property to each control's
> ' tag property
> For Each ctl In Me.Controls
> Select Case ctl.ControlType
> Case acComboBox, acTextBox
> 'MsgBox ctl.Name
> If Not IsNull(ctl) Then
>
> ctl.Tag = ctl
> Else
> ctl.Tag = ""
> End If
>
> End Select
> Next ctl
> ' Do the same for the subform's fields
> For Each ctl In Me!StudySpec.Form
> Select Case ctl.ControlType
> Case acComboBox, acTextBox
> 'MsgBox ctl.Name
> If Not IsNull(ctl) Then
> ctl.Tag = ctl
> Else
> ctl.Tag = ""
> End If
> End Select
> Next ctl
> Else
>
> ' Change the font color and caption
> ' of the cmdEditUndo button
> .Caption = "Change This Record"
> .ForeColor = vbBlue
> ' Change the caption of the form Headers
> lblHeader1.Caption = "View Study "
> lblHeader2.Caption = "View Study "
> ' Undo any changes by copying the data from
> ' the control's Tag property to the control's
> ' Control Source property
> For Each ctl In Me.Controls
> Select Case ctl.ControlType
> Case acComboBox, acTextBox
> 'MsgBox ctl.Name
> If Len(ctl.Tag) > 0 And ctl.Name <> "StudyID" Then
> ctl = ctl.Tag
> ctl.Tag = ""
> ElseIf ctl.Name <> "StudyID" Then
> ctl = " "
> End If
> End Select
> Next ctl
> ' Do the same for the subform's fields
> For Each ctl In Me!StudySpec.Form
> Select Case ctl.ControlType
> Case acComboBox, acTextBox
> ' MsgBox ctl.Name
> If Len(ctl.Tag) > 0 Then
> ctl = ctl.Tag
> ctl.Tag = ""
> ElseIf ctl.Name <> "duedate" Then
> ctl = " "
> End If
> End Select
> Next ctl
> ' Do not allow editing of the main and sub forms
> Call cmdSave_Click
> ' Call FormPermits(False)
> 'MsgBox "else"
> End If
> End With
>
> Exit_cmdEditUndo_Click:
>
> Exit Sub
>
> Err_cmdEditUndo_Click:
> MsgBox Err.Description
> Resume Exit_cmdEditUndo_Click
>
> End Sub
>
>
>
> 'Private Sub Form_BeforeUpdate(Cancel As Integer)
> 'Me.txtNTID.Value = GetNTUserID
> 'End Sub
>
>
> Private Sub Form_Current()
> On Error GoTo Form_Current_ERR
>
> ' Change the cmdEditUndo caption and
> ' font color. Change the form's headers
> ' captions and do not permit editing of
> ' any records when we move to another
> ' record.
>
> If Me.AllowDeletions = True Then
> cmdEditUndo.Visible = False
> lblHeader1.Caption = "Add Study Data "
> lblHeader2.Caption = "Add Study Data "
> ElseIf Me.AllowEdits = True Then
> Call FormPermits(False)
> With cmdEditUndo
> .Caption = "Change This Record"
> .ForeColor = vbBlue
> End With
> ' Hide the cmdSave button if it is visible
> cmdSave.Visible = False
> lblHeader1.Caption = "View Study "
> lblHeader2.Caption = "View Study "
> Me.AllowEdits = False
> End If
>
> Form_Current_EXIT:
> Exit Sub
>
> Form_Current_ERR:
> MsgBox Err.Description
> Resume Form_Current_EXIT
>
> End Sub
> Private Sub cmdClose_Click()
> On Error GoTo Err_cmdClose_Click
>
> DoCmd.Close
>
> Exit_cmdClose_Click:
> Exit Sub
>
> Err_cmdClose_Click:
> MsgBox Err.Description
> Resume Exit_cmdClose_Click
>
> End Sub
>
> Private Sub cmdSave_Click()
> On Error GoTo Err_cmdSave_Click
>
> DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
> With cmdEditUndo
> .Caption = "Change This Record"
> .ForeColor = vbBlue
> cmdEditUndo.Visible = True
> End With
> lblHeader1.Caption = "View Study"
> lblHeader2.Caption = "View Study"
> Call FormPermits(False)
> cmdEditUndo.Visible = True
> Exit_cmdSave_Click:
>
>
> cmdSave.Visible = False
>
> Exit Sub
>
> Err_cmdSave_Click:
> MsgBox Err.Description
> Resume Exit_cmdSave_Click
>
> End Sub
>
>
>
>
> Private Sub Command44_Click()
> On Error GoTo Err_Command44_Click
> If Command44 = True Then
> Me.AllowAdditions = True
> Else: Me.AllowAdditions = False
> DoCmd.GoToRecord , , acNewRec
> End If
>
>
>
> Exit_Command44_Click:
> Exit Sub
>
> Err_Command44_Click:
> MsgBox Err.Description
> Resume Exit_Command44_Click
>
> End Sub
>
|
|
 |