Introduction to PowerBuilder

HomePrevious Lesson: The Print CommandButton
Next Lesson: Error Signaling

The cb_Save_to_Log CommandButton

This button allows the user to append the latest error information to an error log. To accomplish this task, we need to do several jobs. These jobs include opening an external log file, appending information in it and then closing the file.

The script for this task is complicated:
// Object: cb_Save_To_Log in w_error
// Event: Clicked
Int lFIleHandle
lFIleHandle = FileOpen("Product.Err", LineMode!, Write!,&
                        LockWrite!, Append!)
If lFileHandle = -1 Then 
   MessageBox("Save to Log","Unable to open " + &
        " Product.Err file!")
   Return 0
End If
/* Some databases accept userid and some expect logid. If 
   you take care of the checking, you can use this script
   in any application, irrespective of the database 
   connection.*/
If SQLCA.UserId <> "" Then
   FileWrite( lFileHandle,"User: " + SQLCA.UserId)
Else
   FileWrite( lFileHandle,"User: " + SQLCA.LogId)
End If
FileWrite( lFileHandle, "Time:" + String(String(Now(), &
           "m-d-yy h:mm am/pm")))
FileWrite( lFileHandle, "Error No:" + String(&
           dw_error_info.GetItemNumber( 1, "Error_No")))
FileWrite( lFileHandle, "Error Message:" + &
           dw_error_info.GetItemString( 1, "Error_Message"))
FileWrite( lFileHandle, "Error Line No:" + &
  String(dw_error_info.GetItemNumber(1,"Error_Line_No")))
FileWrite( lFileHandle, "Error Window Menu:" + &
      dw_error_info.GetItemString( 1, "Error_WindowMenu"))
FileWrite( lFileHandle, "Error Object: " + &
           dw_error_info.GetItemString( 1, "Error_Object"))
FileWrite( lFileHandle, "Error Object Event: " + &
  dw_error_info.GetItemString( 1, "Error_Object_Event"))
FileClose( lFileHandle)

The first section of code opens a log file 'product.err' and enables linemode, which means each FileWrite() will put text on a new line. We also enable writing and appending to enable us to put text into the log and also to ensure that each line of text is appended to the file. The LockWrite! option ensures that when the file is open, nobody else can write to it.

The next six lines of code writes the UserId or LogId into the file depending on whichever is relevant and then writes in the date and time.

GetItemString() returns the specified field value. Use this function if the field is of string datatype. If the field is of number type, use GetItemNumber(). Similarly for date fields, call GetItemDate(). All these functions take two parameters, the row number and the field number/name.
HomePrevious Lesson: The Print CommandButton
Next Lesson: Error Signaling