Online Book Reader

Home Category

Access Cookbook - Ken Getz [123]

By Root 2107 0
lpBuffer As String, nSize As Long) As Long

Private Const acbcMaxComputerName = 15

Public Function acbComputerName( ) As String

' Retrieve the name of the computer.

Dim strBuffer As String

Dim lngLen As Long

strBuffer = Space(acbcMaxComputerName + 1)

lngLen = Len(strBuffer)

If CBool(GetComputerName(strBuffer, lngLen)) Then

acbComputerName = Left$(strBuffer, lngLen)

Else

acbComputerName = ""

End If

End Function

Another option is to create your own public function called CurrentUser that returns the network name. That way, you won't need to change any of the code that calls CurrentUser in your forms. Access will use your function rather than the built-in one, and if you do implement Access security, all you need to do is rename or remove the custom CurrentUser function to have the form code start retrieving Access security names using the built-in CurrentUser function.

See Also


For more information on using DAO in Access databases, see How Do I Use Data Access Objects (DAO) in New Databases? in the Preface.

6.2. Determine if You're on a New Record in a Form


Problem


Often, you need to do different things depending on whether the current row is the "new" row on a form. For example, you might want to display a certain message box only when adding records. How can you do this?

Solution


You can use a form's NewRecord property to determine if you are on a new record by checking its value from an event procedure attached to the OnCurrent event property or some other event property of the form.

Follow these steps to implement this functionality in your own forms:

Create a new form or modify the design of an existing form.

Create an event procedure for the form's Current event. In that event procedure, create an If...Then statement that will branch based on the value of the form's NewRecord property. The code of the event procedure should look like this:

Private Sub Form_Current( )

If Me.NewRecord Then

' Do something for a new record.

Else

' Do something for an existing record.

End If

End Sub

You may wish to alter some visual cue on the form to indicate whether you are on a new record. For example, you might change the text of a label, the text of the form's titlebar, or the picture of an image control. In the sample form, we changed the picture of an image control in the form's header, imgFlag, by copying the picture from one of two hidden image controls that are also located on the form. The final Current event procedure looks like this:

Private Sub Form_Current( )

' Determine if this is a new record and change the bitmap

' of the imgFlag control to give the user visual feedback.

' See the Solution in Recipe 9.7 for an explanation of using the

' PictureData property.

If Me.NewRecord Then

Me.imgFlag.PictureData = Me.imgFlagNew.PictureData

Else

Me.imgFlag.PictureData = Me.imgFlagEdit.PictureData

End If

End Sub

Create any additional code that reacts to the NewRecord property. In the sample form, we decided to remind the user to log in the new record when saving it. Thus, we created the following event procedure attached to the form's BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim strMsg As String

If Me.NewRecord Then

strMsg = "You just added a new record " & _

"(# " & Me.ContactID & ")" & vbCrLf & _

"Please don't forget to log it in!"

Beep

MsgBox strMsg, vbOKOnly + vbInformation, "New Record Added"

End If

End Sub

To see an example, load and open frmContacts from 06-02.MDB. Notice that the picture in the upper-left corner of the form changes to indicate whether you are editing an existing record (Figure 6-3) or adding a new record (Figure 6-4). In addition, when you save a newly added record, a message box is displayed that reminds you to log the new record (Figure 6-4). The message box does not appear when you save changes to an existing record.

Figure 6-3. The sample form indicates that you are editing an existing record

Figure 6-4. The sample form indicates that you are adding a record

Return Main Page Previous Page Next Page

®Online Book Reader