Link to home
Start Free TrialLog in
Avatar of andygerman
andygerman

asked on

Pointers to bad code

Hi All

I have an interview on friday for a VB programming role.  I have quite a lot of experience in C++, ABAP, SQL but not much in VB and am scratching around for some sample code to present.

The code below is taken from a form I designed to interface with a fixed barcode scanner, a label printer and SAP.  Could anyone offer any critique or advice?  Any obvious glaring problems?  Any good bits I should expand on?

This is quite a tall order but the points are there to entice.

Option Explicit
'******************************************************************************
'* Begin local global variables
'******************************************************************************
Private g_iDialnr As Long
Private g_bOk As Boolean
Private g_PreviousActionStatus As Long
Private g_CurrentActionStatus As Long
Private g_bPalletWindowOpen As Boolean
Private g_iPostingTimeout As Long   'The length in seconds of the 'Window'
                                    'for editing the scanned and retreived data
Private g_time As Integer           'Seconds remaining
Private g_Received As String
Private g_LampOn As Boolean
Private g_PrintLocal As Boolean
Private g_FieldsLocked As Boolean
Private g_TopPRN As String
Private g_BottomPRN As String
Private g_LocalPRN As String
'******************************************************************************
'* End local global variables
'******************************************************************************
Private Const NFAUTORECEIPT_INBOUNDCOM_VALID As Long = 1000
Private Const NFAUTORECEIPT_INBOUNDCOM_NOCODES As Long = 1001
Private Const NFAUTORECEIPT_INBOUNDCOM_MULTIPLECODES As Long = 1002
Private Const NFAUTORECEIPT_SAPOK_VALID As Long = 1010
Private Const NFAUTORECEIPT_SAPOK_ERROR As Long = 1011
Private Const NFAUTORECEIPT_SAPERR_FAILED As Long = 1012
Private Const NFAUTORECEIPT_IDOC_OK As Long = 1020
Private Const NFAUTORECEIPT_IDOC_FAIL As Long = 1021
Private Const NFAUTORECEIPT_PALLETWINDOWTIMEOUT As Long = 1030
Private Const NFAUTORECEIPT_ERRORPOST_SUCCESSFUL As Long = 1040
Private Const NFAUTORECEIPT_ERRORPOST_FAILED As Long = 1041
Private Const NFAUTORECEIPT_RESET As Long = 1050
Private Const NFAUTORECEIPT_MANSCAN_VALID As Long = 1060
Private Const NFAUTORECEIPT_MANSCAN_INVALID As Long = 1061
Private Const NFAUTORECEIPT_INI_FAILED As Long = 1070

Private Const LBL_EAN_NOT_FOUND As String = "EAN Not found in SAP"
Private Const LBL_SRFC_FAILED   As String = "SAP function call failed"
Private Const LBL_NOTHING_SCANNED   As String = "The scanners detected no barcodes"
Private Const LBL_MULTIPLE_EAN  As String = "Multiple EAN codes were detected"
Private Const LBL_IDOC_FAILURE  As String = "I-Doc transmission failed"
Private Const LBL_GENERAL_ERROR As String = "Unidentified error"

'******************************************************************************
'* IFC_Handle(...)
'* Description
'*  Handles Interform communication messages
'* Parameters
'*  [I] hIFC    handle of caller
'*  [I] szCmd   command to handle
'* Returns
'*  IFC_OK if successfull
'*  IFC_ERR or otherwise if not
'******************************************************************************
Public Function IFC_Handle( _
    hIFC As Long, _
    szCmd As String) As Long
Select Case szCmd
    Case IFC_CMD_FRMOPEN
        Call IFC_PARA_Get(hIFC, IFC_PARA_DIALNR, g_iDialnr)
    Case IFC_CMD_CTXMENU
    Case IFC_CMD_LANGUAGE
    Case IFC_CMD_OVERPICKING
    Case IFC_CMD_CMD_CMD
    Case IFC_CMD_CMD_MISC
End Select
End Function

Private Sub cmd_printlocal_Click()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: cmd_printlocal_Click"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    If (g_PrintLocal) Then
        g_PrintLocal = False
        Me.cmd_printlocal.Caption = "Print to Local"
        Me.cmd_printlocal.BackColor = &H8000000F
        Me.lbl_topprn.Visible = True
        Me.txt_botprn.Visible = True
        Me.txt_topprn.Visible = True
        PrinterImage.Visible = False
    Else
        g_PrintLocal = True
        Me.cmd_printlocal.Caption = "Print to Auto"
        Me.cmd_printlocal.BackColor = &H0&
        Me.lbl_topprn.Visible = False
        Me.txt_botprn.Visible = False
        Me.txt_topprn.Visible = False
        PrinterImage.Visible = True
    End If
End Sub

Private Sub Form_Load()
    AutoScanINI
End Sub
Sub ParseScanData(ByVal InString As String)
'this strips out the scanned data from the string coming in
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: ParseScanData:  " & InString
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
   Dim temp As String
   Dim x As Long
   Dim iStart As Long
   Dim iEnd As Long
   Dim OutString As String
   Dim iHashPos As Long
   Dim iStartPos As Long
   Dim iEndPos As Long
   Dim iScanIndicator As Long
   Dim iEtx As Long
   iHashPos = InStr(InString, "#")
   iStartPos = InStr(InString, "(")
   iEndPos = InStr(InString, ")")
   iScanIndicator = InStr(InString, ">")
   iEtx = InStr(InString, Chr$(3))
   If (iScanIndicator > 0) Then
        If (iEndPos > 0) Then
             If (iHashPos > 0) Then
                 'there is more than one barcode
                 ActionStatusChange (NFAUTORECEIPT_INBOUNDCOM_MULTIPLECODES)
             Else
                 If ((iEndPos - iStartPos) = 1) Then
                    'No barcodes were returned
                    ActionStatusChange (NFAUTORECEIPT_INBOUNDCOM_NOCODES)
                 Else
                    'There appears to be just one barcode
                    OutString = Mid(InString, iStartPos + 1, (iEndPos - iStartPos) - 1)
                    ParseBarcode (OutString)
                 End If
             End If
         End If
    Else
        If (iEtx > 0) Then
            'Ignore it, it is an output string echo
        Else
            'No scan indicator present so this is a manual scan input
            OutString = InString
            ParseBarcode (OutString)
        End If
    End If
End Sub
Sub ParseBarcode(Code As String)
'This takes the ean number from the barcode
    Dim iLength As Integer
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: ParseBarcode:  " & Code
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    iLength = Len(Code)
    Call INI_GetSection(g_WSPC.szMDEVB_INIFile, "CODE_CONFIG")
    Select Case iLength
       Case 14
          'do nothing this is the correct length
       Case 27
          'The relevant EAN code is embedded within the scan - parse
              Code = Mid(Code, 6, 14)
       Case Else
        'Do nothing pass the string direct to the form
    End Select
    'Pass the barcode back to the form
    Me.txt_ean.Text = Code
    ActionStatusChange (NFAUTORECEIPT_INBOUNDCOM_VALID)
End Sub

Sub GetPODataFromSAP()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: GetPODataFromSAP"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    Dim szPONum As String * ZAUTORDERS_AUFNR_LN
    Dim szMatnr As String * ZAUTORDERS_MATNR_LN
    Dim szMatdesc As String * ZAUTORDERS_MAKTX_LN
    Dim szBotQty As String * ZAUTORDERS_Z_BOTQTY_LN
    Dim szBotPrt As String * ZAUTORDERS_Z_BOTPRT_LN
    Dim szTopQty As String * ZAUTORDERS_Z_TOPQTY_LN
    Dim szTopPrt As String * ZAUTORDERS_Z_TOPPRT_LN
    Dim szLeType As String * ZAUTORDERS_LETYP_LN
    Dim szE_Result As String * 4
    Dim iError As Long
    AddEANtoCombo (Me.txt_ean.Text)
    Me.lbl_status.Caption = "Calling SAP for PO details..."
    Me.lbl_status.Refresh
    Me.txt_ean.Refresh
    On Error GoTo err_GetPODataFromSAP
        Call MOB_GUIMDE_SRFC_NFSRFCGetPODetailsForEAN( _
            ByVal STR_VbToC(Me.txt_ean.Text), _
            ByVal szPONum, _
            ByVal szMatnr, _
            ByVal szMatdesc, _
            ByVal szBotQty, _
            ByVal szBotPrt, _
            ByVal szTopQty, _
            ByVal szTopPrt, _
            ByVal szLeType, _
            ByVal szE_Result, _
            iError)
        Select Case iError
            Case MOB_GUIMDE_SRFC_OK
                Me.txt_ponum.Text = STR_CToVb(szPONum)
                Me.txt_matnr.Text = STR_CToVb(szMatnr)
                Me.txt_matdesc.Text = STR_CToVb(szMatdesc)
                Me.txt_botqty.Text = Val(STR_CToVb(szBotQty))
                Me.txt_botprn.Text = STR_CToVb(szBotPrt)
                Me.txt_topqty.Text = Val(STR_CToVb(szTopQty))
                Me.txt_topprn.Text = STR_CToVb(szTopPrt)
                Me.txt_sutype.Text = STR_CToVb(szLeType)
                Call ActionStatusChange(NFAUTORECEIPT_SAPOK_VALID)
            Case MOB_GUIMDE_SRFC_ERR
                Call ActionStatusChange(NFAUTORECEIPT_SAPERR_FAILED)
            Case MOB_GUIMDE_SRFC_ERR_SAP
                Call ActionStatusChange(NFAUTORECEIPT_SAPOK_ERROR)
        End Select
    Exit Sub
err_GetPODataFromSAP:
        Me.lbl_status.Caption = "Call to dllmobguimde failed: " & Err.Number & ": " & Err.Description & ": Source: " & Err.Source
End Sub
Sub SendIDOC()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: SendIDOC"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    'Sap function call
    On Error GoTo err_SendIDoc
    Me.lbl_status.Caption = "Transmitting I-Doc..."
    Me.lbl_status.Refresh
    Dim iError As Long
    If (Not g_PrintLocal) Then
        Call MOB_GUIMDE_GM_NFRcptIDocAS( _
            STR_VbToC(Me.txt_matnr.Text), _
            STR_VbToC(Me.txt_ponum.Text), _
            STR_VbToC(Me.txt_topprn.Text), _
            STR_VbToC(Me.txt_botprn.Text), _
            STR_VbToC(Me.txt_ean.Text), _
            STR_VbToC(Me.txt_sutype.Text), _
            Val(Me.txt_topqty.Text), _
            Val(Me.txt_botqty.Text), _
            iError)
    Else
        Call MOB_GUIMDE_GM_NFRcptIDocAS( _
            STR_VbToC(Me.txt_matnr.Text), _
            STR_VbToC(Me.txt_ponum.Text), _
            STR_VbToC(""), _
            STR_VbToC(""), _
            STR_VbToC(Me.txt_ean.Text), _
            STR_VbToC(Me.txt_sutype.Text), _
            Val(Me.txt_topqty.Text), _
            Val(Me.txt_botqty.Text), _
            iError)
    End If
        Select Case iError
            Case MOB_GUIMDE_GM_OK
                ActionStatusChange (NFAUTORECEIPT_IDOC_OK)
            Case MOB_GUIMDE_GM_ERR
                ActionStatusChange (NFAUTORECEIPT_IDOC_FAIL)
        End Select
Exit Sub
err_SendIDoc:
    Me.lbl_status.Caption = "Call to dllmobguimde failed: " & Err.Number & ": " & Err.Description & ": Source: " & Err.Source
End Sub

Private Sub Grid1_RowColChange()

End Sub

Private Sub MSCommScanners_OnComm()
   Dim szLogTxt As String
   Dim InChar As String
   Select Case MSCommScanners.CommEvent
   ' Errors
      Case comEventBreak   ' A Break was received.
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventBreak = A break was received."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
      Case comEventCDTO    ' CD (RLSD) Timeout.
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventCDTO =  CD (RLSD) Timeout."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
      Case comEventCTSTO   ' CTS Timeout.
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventCTSTO =   CTS Timeout."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
      Case comEventDSRTO   ' DSR Timeout.
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventDSRTO =   DSR Timeout."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
      Case comEventFrame   ' Framing Error
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventFrame =   Framing Error."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
      Case comEventOverrun ' Data Lost.
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventOverrun =   Data Lost."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
      Case comEventRxOver  ' Receive buffer overflow.
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventRxOver =   Receive buffer overflow."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
      Case comEventRxParity   ' Parity Error.
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventRxParity =   Parity Error."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
      Case comEventTxFull  ' Transmit buffer full.
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventTxFull =   Transmit buffer full."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
      Case comEventDCB  ' Unexpected error retrieving DCB]
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEventDCB =   Unexpected error retrieving DCB."
            Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
   ' Events
      Case comEvCD   ' Change in the CD line.
      Case comEvCTS  ' Change in the CTS line.
      Case comEvDSR  ' Change in the DSR line.
      Case comEvRing ' Change in the Ring Indicator.
      Case comEvReceive ' Received RThreshold # of chars.
            InChar = MSCommScanners.Input
            szLogTxt = "FUNCTION: MSCommScanners_OnComm: comEvReceive << " & InChar
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            Select Case InChar
                Case Chr$(2) 'STX
                    ' Clear g_Received so it's ready for the next line
                    g_Received = InChar
                Case Chr$(3) 'ETX
                    g_Received = g_Received & InChar
                    Call ParseScanData(g_Received)
                Case Else
                    ' We concatanate what we have received to what has
                    ' already been received
                    g_Received = g_Received & InChar
            End Select
      Case comEvSend ' There are SThreshold number of
                     ' characters in the transmit
                     ' buffer.
      Case comEvEOF  ' An EOF character was found in
                     ' the input stream.
   End Select
End Sub



Private Sub txt_botprn_GotFocus()
    txt_botprn.SelStart = 0
    txt_botprn.SelLength = Len(txt_botprn.Text)
End Sub

Private Sub txt_botqty_GotFocus()
    txt_botqty.SelStart = 0
    txt_botqty.SelLength = Len(txt_botqty.Text)
End Sub

Private Sub txt_ean_KeyPress(KeyAscii As Integer)
    If (KeyAscii = 13) Then
        cmd_manscan_Click
    End If
End Sub

Private Sub txt_topprn_GotFocus()
    txt_topprn.SelStart = 0
    txt_topprn.SelLength = Len(txt_topprn.Text)
End Sub

Private Sub txt_topqty_GotFocus()
    txt_topqty.SelStart = 0
    txt_topqty.SelLength = Len(txt_topqty.Text)
End Sub

Private Sub Window_Timer()
    Me.lbl_timeleft.Caption = g_time & " Seconds Remaining"
    g_time = g_time - 1
    If (g_time < 1) Then
        ExecutePost
    End If
End Sub
Private Sub ActionStatusChange(NewStatus As Long)
    Dim szLogTxt As String
    g_PreviousActionStatus = g_CurrentActionStatus
    g_CurrentActionStatus = NewStatus
    Select Case g_CurrentActionStatus
        Case NFAUTORECEIPT_INBOUNDCOM_VALID
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_INBOUNDCOM_VALID"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            'Inbound communications valid,
            'Initialise the state
            LockAndHide
            'PALLET WINDOW:         Open it
            OpenWindow
            'FIELDS TO POPULATE:    EAN should already be populated
            'FIELDS TO UNLOCK:      NONE
            'FOCUS:                 NONE
            'STATUS:                "calling SAP"
            'ACTION:                Make the call to SAP
            GetPODataFromSAP
            'WARNING LAMP:          OFF
            LampOff
            'BUTTONS:               NONE
        Case NFAUTORECEIPT_INBOUNDCOM_NOCODES
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_INBOUNDCOM_NOCODES"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'PALLET WINDOW:         Open it
            OpenWindow
            'FIELDS TO POPULATE:    NONE
            'FIELDS TO UNLOCK:      EAN
            Me.txt_ean.Locked = False
            'FOCUS:                 EAN
            Me.txt_ean.SetFocus
            'STATUS:                "The scanners have miscanned, scan manually"
            Me.lbl_status.Caption = "No barcodes detected, scan manually for this pallet."
            'ACTION:                Wait for user input
            'WARNING LAMP:          ON
            LampOn
            'BUTTONS:               Manual Scan
            Me.cmd_manscan.Visible = True
        Case NFAUTORECEIPT_INBOUNDCOM_MULTIPLECODES
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_INBOUNDCOM_MULTIPLECODES"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'PALLET WINDOW:         Open it
            OpenWindow
            'FIELDS TO POPULATE:    NONE
            'FIELDS TO UNLOCK:      EAN
            Me.txt_ean.Locked = False
            'FOCUS:                 EAN
            Me.txt_ean.SetFocus
            'STATUS:                "The scanners have miscanned, scan manually"
            Me.lbl_status.Caption = "Multiple barcodes detected, scan manually"
            'ACTION:                Wait for user input
            'WARNING LAMP:          ON
            LampOn
            'BUTTONS:               Manual Scan
            Me.cmd_manscan.Visible = True
        Case NFAUTORECEIPT_MANSCAN_VALID
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_MANSCAN_VALID"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            'Set status to INBOUND COM VALID
            ActionStatusChange (NFAUTORECEIPT_INBOUNDCOM_VALID)
        Case NFAUTORECEIPT_MANSCAN_INVALID
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_MANSCAN_INVALID"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            'Set status to NFAUTORECEIPT_INBOUNDCOM_NOTVALID
            ActionStatusChange (NFAUTORECEIPT_INBOUNDCOM_NOCODES)
        Case NFAUTORECEIPT_SAPOK_VALID
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_SAPOK_VALID"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'Call to SAP was successful
            'Initialise the state
            'PALLET WINDOW:         Continues
            'FIELDS TO POPULATE:    ALL except EAN
            'FIELDS TO UNLOCK:      Top and Bottom Qty
            Me.txt_botqty.Locked = False
            Me.txt_topqty.Locked = False
            Me.txt_botprn.Locked = False
            Me.txt_topprn.Locked = False
            'FOCUS:                 Top Qty
            Me.txt_topqty.SetFocus
            'STATUS:                "Sap call valid, make changes to qty before posting"
            Me.lbl_status.Caption = "Sap call valid, make changes to qty before posting"
            'ACTION:                Wait for user input
            'WARNING LAMP:          OFF
            LampOff
            'BUTTONS:               Post Now
            Me.cmd_postnow.Visible = True
            Me.cmd_reset.Visible = True
        Case NFAUTORECEIPT_SAPOK_ERROR
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_SAPOK_ERROR"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'Call to SAP was successful but reported an error
            'Initialise the state
            'PALLET WINDOW:         Continues
            'FIELDS TO POPULATE:    N/A
            'FIELDS TO UNLOCK:      NONE
            'FOCUS:                 NONE
            'STATUS:                "SAP does not recognise the EAN scanned"
            Me.lbl_status.Caption = "SAP does not recognise the EAN scanned"
            'ACTION:                Wait for user input
            'WARNING LAMP:          ON
            LampOn
            'BUTTONS:               Retry, Post Now
            Me.cmd_retry.Visible = True
            Me.cmd_manscan.Visible = True
            Me.cmd_postnow.Visible = True
            Me.cmd_reset.Visible = True
        Case NFAUTORECEIPT_SAPERR_FAILED
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_SAPERR_FAILED"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'The call to SAP failed
            'Initialise the state
            'PALLET WINDOW:         Continues
            'FIELDS TO POPULATE:    N/A
            'FIELDS TO UNLOCK:      NONE
            'FOCUS:                 NONE
            'STATUS:                "Unable to connect to SAP"
            Me.lbl_status.Caption = "Unable to connect to SAP"
            'ACTION:                Wait for user input
            'WARNING LAMP:          ON
            LampOn
            'BUTTONS:               Retry, Post Now
            Me.cmd_retry.Visible = True
            Me.cmd_postnow.Visible = True
            Me.cmd_reset.Visible = True
        Case NFAUTORECEIPT_IDOC_OK
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_IDOC_OK"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'I-Doc posted OK
            'Initialise the state
            'PALLET WINDOW:         Close the window
            CloseWindow
            'FIELDS TO POPULATE:    Clear all fields
            ClearAllFields
            'FIELDS TO UNLOCK:      NONE
            'FOCUS:                 NONE
            'STATUS:                "I-Doc transmitted successfully"
            Me.lbl_status.Caption = "I-Doc transmitted successfully"
            'ACTION:                Wait for user input
            'WARNING LAMP:          OFF
            LampOff
            'BUTTONS:               Reset
            Me.cmd_manscan.Visible = True
            Me.cmd_reset.Visible = True
            Me.cmd_reset.Visible = True
        Case NFAUTORECEIPT_IDOC_FAIL
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_IDOC_FAIL"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'I-Doc posting failed
            'Initialise the state
            'PALLET WINDOW:         Continues
           
            'FIELDS TO POPULATE:    N/A
            'FIELDS TO UNLOCK:      NONE
            'FOCUS:                 NONE
            'STATUS:                "I-Doc transmission failed"
            Me.lbl_status.Caption = "I-Doc transmission failed"
            'ACTION:                Wait for user input
            'WARNING LAMP:          ON
            LampOn
            'BUTTONS:               Retry
            Me.cmd_retry.Visible = True
            Me.cmd_postnow.Visible = True
            Me.cmd_reset.Visible = True
        Case NFAUTORECEIPT_ERRORPOST_SUCCESSFUL
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_ERRORPOST_SUCCESSFUL"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'User clicks on the post now button
            'Initialise the state
            'PALLET WINDOW:         Continues
            CloseWindow
            'FIELDS TO POPULATE:    N/A
            'FIELDS TO UNLOCK:      NONE
            'FOCUS:                 NONE
            'STATUS:                "Error posting successful"
            Me.lbl_status.Caption = "Error posting successful"
            'ACTION:                Wait for user input
            'WARNING LAMP:          OFF
            LampOff
            'BUTTONS:
            Me.cmd_manscan.Visible = True
            Me.cmd_reset.Visible = True
    Case NFAUTORECEIPT_ERRORPOST_FAILED
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_ERRORPOST_FAILED"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'I-Doc posting failed
            'Initialise the state
            'PALLET WINDOW:         Continues
           
            'FIELDS TO POPULATE:    N/A
            'FIELDS TO UNLOCK:      NONE
            'FOCUS:                 NONE
            'STATUS:                "I-Doc transmission failed"
            'ACTION:                Wait for user input
            'WARNING LAMP:          ON
            LampOn
            'BUTTONS:               Retry
            Me.cmd_retry.Visible = True
            Me.cmd_postnow.Visible = True
            Me.cmd_reset.Visible = True
        Case NFAUTORECEIPT_RESET
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_RESET"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            'User clicks on the reset button
            'Initialise the state
            'PALLET WINDOW:         Ends
            CloseWindow
            'FIELDS TO POPULATE:    Clear all
            ClearAllFields
            'FIELDS TO UNLOCK:      NONE
            'FOCUS:                 NONE
            'STATUS:                ""
            Me.lbl_status.Caption = ""
            'ACTION:
            'WARNING LAMP:          OFF
            LampOff
            'BUTTONS:
            Me.cmd_manscan.Visible = True
        Case NFAUTORECEIPT_INI_FAILED
            szLogTxt = "FUNCTION: ActionStatusChange: New Status = NFAUTORECEIPT_INI_FAILED"
            Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
            LockAndHide
            ClearAllFields
            Me.lbl_status.Caption = "ERROR: Unable to find 'AUTOSCAN' section in the ini file"
            Me.cmd_retry.Visible = True
            Me.cmd_reset.Visible = True
    End Select
End Sub
Private Sub cmd_manscan_Click()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: cmd_manscan_Click"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    ParseScanData (Me.txt_ean.Text)
End Sub
Private Sub cmd_postnow_Click()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: cmd_postnow_Click"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    LockAndHide
    Me.lbl_status.Caption = "Attempting to post"
    ExecutePost
End Sub
Private Sub cmd_reset_Click()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: cmd_reset_Click"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    ActionStatusChange (NFAUTORECEIPT_RESET)
End Sub
Private Sub cmd_retry_Click()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: cmd_retry_Click"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    Select Case g_CurrentActionStatus
        Case NFAUTORECEIPT_SAPOK_ERROR
            GetPODataFromSAP
        Case NFAUTORECEIPT_SAPERR_FAILED
            GetPODataFromSAP
        Case NFAUTORECEIPT_IDOC_FAIL
            SendIDOC
        Case NFAUTORECEIPT_INI_FAILED
            AutoScanINI
    End Select
End Sub
Private Sub cmd_unlockfields_Click()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: cmd_unlockfields_Click"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    If (g_FieldsLocked) Then
        'Unlock the fields
        g_FieldsLocked = False
        Me.txt_botprn.Locked = False
        Me.txt_botqty.Locked = False
        Me.txt_ean.Locked = False
        Me.txt_matdesc.Locked = False
        Me.txt_matnr.Locked = False
        Me.txt_ponum.Locked = False
        Me.txt_sutype.Locked = False
        Me.txt_topprn.Locked = False
        Me.txt_topqty.Locked = False
        Me.cmd_unlockfields.Picture = LoadPicture(g_WSPC.szMDEVB_RootPath & "\bitmap\LockFields.bmp")
    Else
        'Lock the fields
        g_FieldsLocked = True
        Me.txt_botprn.Locked = True
        Me.txt_botqty.Locked = True
        Me.txt_ean.Locked = True
        Me.txt_matdesc.Locked = True
        Me.txt_matnr.Locked = True
        Me.txt_ponum.Locked = True
        Me.txt_sutype.Locked = True
        Me.txt_topprn.Locked = True
        Me.txt_topqty.Locked = True
        Me.cmd_unlockfields.Picture = LoadPicture(g_WSPC.szMDEVB_RootPath & "\bitmap\UnlockFields.bmp")
    End If
End Sub
Sub OpenWindow()
    g_time = g_iPostingTimeout
    Me.Window.Enabled = True
End Sub
Sub CloseWindow()
    ClearAllFields
    g_time = 0
    Me.Window.Enabled = False
    Me.lbl_timeleft.Caption = ""
End Sub
Sub LockAndHide()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: LockAndHide"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    Me.txt_botprn.Locked = True
    Me.txt_botqty.Locked = True
    Me.txt_ean.Locked = True
    Me.txt_matdesc.Locked = True
    Me.txt_matnr.Locked = True
    Me.txt_ponum.Locked = True
    Me.txt_sutype.Locked = True
    Me.txt_topprn.Locked = True
    Me.txt_topqty.Locked = True
    Me.cmd_manscan.Visible = False
    Me.cmd_postnow.Visible = False
    Me.cmd_reset.Visible = False
    Me.cmd_retry.Visible = False
    'Me.cmd_unlockfields.Visible = False
    g_FieldsLocked = True
    Me.cmd_unlockfields.Picture = LoadPicture(g_WSPC.szMDEVB_RootPath & "\bitmap\UnlockFields.bmp")
    LampOff
End Sub
Sub LampOn()
    If (g_LampOn = False) Then
        g_LampOn = True
        Dim szLogTxt As String
        Dim Cmnd As String
        szLogTxt = "FUNCTION: LampOn"
        Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
        On Error Resume Next
        Cmnd = Chr$(2) + "290001" + Chr$(3)
        MSCommScanners.Output = Cmnd '
       
    End If
End Sub
Sub LampOff()
    If (g_LampOn = True) Then
        g_LampOn = False
        Dim szLogTxt As String
        Dim Cmnd As String
        szLogTxt = "FUNCTION: LampOff"
        Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
        On Error Resume Next
        Cmnd = Chr$(2) + "290000" + Chr$(3)
        MSCommScanners.Output = Cmnd '
    End If
End Sub
Sub ExecutePost()
    'this function executes the posting of either the i-doc or the error label
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: ExecutePost"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    Select Case g_CurrentActionStatus
        Case NFAUTORECEIPT_SAPOK_VALID
            SendIDOC
        Case NFAUTORECEIPT_SAPOK_ERROR
            PrintLabel (LBL_EAN_NOT_FOUND)
        Case NFAUTORECEIPT_SAPERR_FAILED
            PrintLabel (LBL_SRFC_FAILED)
        Case NFAUTORECEIPT_INBOUNDCOM_NOCODES
            PrintLabel (LBL_NOTHING_SCANNED)
        Case NFAUTORECEIPT_INBOUNDCOM_MULTIPLECODES
            PrintLabel (LBL_MULTIPLE_EAN)
        Case NFAUTORECEIPT_IDOC_FAIL
            PrintLabel (LBL_IDOC_FAILURE)
        Case NFAUTORECEIPT_MANSCAN_INVALID
            PrintLabel (LBL_NOTHING_SCANNED)
        Case Else
            PrintLabel (LBL_GENERAL_ERROR)
    End Select
End Sub
Sub ClearAllFields()
    Dim szLogTxt As String
    szLogTxt = "FUNCTION: ClearAllFields"
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
    Me.txt_botprn.Text = ""
    Me.txt_botqty.Text = ""
    Me.txt_ean.Text = ""
    Me.txt_matdesc.Text = ""
    Me.txt_matnr.Text = ""
    Me.txt_ponum.Text = ""
    Me.txt_sutype.Text = ""
    Me.txt_topprn.Text = ""
    Me.txt_topqty.Text = ""
End Sub
Sub PrintLabel(LabelName As String)

Dim aPrintStrings(20) As PRINT_STRING_t
Dim j As Integer
For j = LBound(aPrintStrings) To UBound(aPrintStrings)
    aPrintStrings(j).iSize = 18
    aPrintStrings(j).Text = " "
Next

On Error GoTo PrintLabelErr
    Dim szLogTxt As String
    Dim ShellCommand As String
    szLogTxt = "PrintLabel: " & LabelName
    Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
   
    Dim i As Integer
    i = 0
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = " "
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = "-----------------------------------"
    i = i + 1
    aPrintStrings(i).iSize = 30
    aPrintStrings(i).Text = "Error Status!"
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = "Please receipt manually"
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = "-----------------------------------"
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = " "
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = " "
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = LabelName
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = " "
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = " "
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = Now
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = " "
    i = i + 1
    aPrintStrings(i).iSize = 18
    aPrintStrings(i).Text = "----------------------------------"

'Sub SimplePrint(aPrintData() As PRINT_STRING_t, DeviceName As String)
If (Not g_PrintLocal) Then
    'Call SimplePrint(aPrintStrings, g_TopPRN)
    Call SimplePrint(aPrintStrings, g_BottomPRN)
Else
    Call SimplePrint(aPrintStrings, g_LocalPRN)
End If

    ActionStatusChange (NFAUTORECEIPT_ERRORPOST_SUCCESSFUL)
    Exit Sub
PrintLabelErr:
    szLogTxt = "PERMANENT ERROR! PrintLabel: " & LabelName & " " & Err.Number & ":" & Err.Description
    Me.lbl_status.Caption = szLogTxt
    Call LOG_Write(LOG_LEVEL_ERROR, szLogTxt)
    ActionStatusChange (NFAUTORECEIPT_ERRORPOST_FAILED)
End Sub
Sub AutoScanINI()
    Dim szLogTxt As String
    Dim tSectionAutoScan As INI_section_t
    g_PrintLocal = False
    PrinterImage.Visible = False
    g_FieldsLocked = True
    tSectionAutoScan.szName = "AUTOSCAN"
    If (INI_GetSection(g_WSPC.szMDEVB_INIFile, tSectionAutoScan)) Then
        g_iPostingTimeout = INI_GetIntFromSection(tSectionAutoScan, "PostingsTimeout_s")
        g_TopPRN = INI_GetStrFromSection(tSectionAutoScan, "TopPrinter")
        g_BottomPRN = INI_GetStrFromSection(tSectionAutoScan, "BotPrinter")
        g_LocalPRN = INI_GetStrFromSection(tSectionAutoScan, "LocalPrinter")
        With MSCommScanners
             .CommPort = INI_GetIntFromSection(tSectionAutoScan, "ScannerComPort")
             .Handshaking = 2 - comRTS
             .RThreshold = 1
             .InputLen = 1
             .RTSEnable = True
             .Settings = "9600,n,8,1"
             .SThreshold = 8
             .PortOpen = True
            ' Leave all other settings as default values.
        End With
        On Error Resume Next
        szLogTxt = "FUNCTION: AutoscanINI: Initialisation successful"
        Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
        ActionStatusChange (NFAUTORECEIPT_RESET)
    Else
        szLogTxt = "FUNCTION: AutoscanINI: Initialisation failed"
        Call LOG_Write(LOG_LEVEL_INFO, szLogTxt)
        ActionStatusChange (NFAUTORECEIPT_INI_FAILED)
    End If
End Sub

Sub AddEANtoCombo(szEAN As String)
    Dim bMatchFound As Boolean
    Dim Index As Integer
    'Look for a match in the existing combo list
    For Index = 0 To (Me.txt_ean.ListCount - 1)
        If (szEAN = Me.txt_ean.List(Index)) Then
            bMatchFound = True
        End If
    Next Index
    'Add the item to the list if it is not already there
    If (Not bMatchFound) Then
    'If the list length is 10 then remove the first item,
    'remove the oldest item
        If (Me.txt_ean.ListCount = 10) Then
            Me.txt_ean.RemoveItem (0)
        End If
        Me.txt_ean.AddItem (szEAN)
    End If
End Sub
Avatar of Erick37
Erick37
Flag of United States of America image

Just on quick glance:

1. Not very many comments or documentation
2. Unnecessary use of "Me" keyword
Avatar of andygerman
andygerman

ASKER

OK

When does the 'Me' keyword apply and more importantly when does it not apply?
Just don't use "Me" when using control on the same form with your code.
"Me" is usefull when you want to to refer to the specific instance of the class or form where the code is executing.
For example:
Unload Me ' Unload current form

There is another usage of "Me"
Quote from MSDN:
==============================
Shadowing Form Properties and Controls
Due to the effect of shadowing, form properties, controls, constants, and procedures are treated as module-level variables in the form module. It is not legal to have a form property or control with the same name as a module-level variable, constant, user-defined type, or procedure because both are in the same scope.

Within the form module, local variables with the same names as controls on the form shadow the controls. You must qualify the control with a reference to the form or the Me keyword to set or get its value or any of its properties. For example:

Private Sub Form_Click ()
Dim Text1, BackColor
' Assume there is also a control on the form called
' Text1.
   Text1 = "Variable"      ' Variable shadows control.
   Me.Text1 = "Control"   ' Must qualify with Me to get
                        ' control.
   Text1.Top = 0         ' This causes an error!
   Me.Text1.Top = 0      ' Must qualify with Me to get
                        ' control.
   BackColor = 0         ' Variable shadows property.
   Me.BackColor = 0      ' Must qualify with Me to get
                        ' form property.
End Sub
==========================
End of Quote
Using of Me keyword is good programming practice. It allows to differentiate between form functions and properties, and global VB functions (like LoadPicture).

Empty lines between functions and sometimes inside of them make the code more readable.
Nice code!
Just may be one more thing:
Instead of using

cmd_unlockfields.Picture = LoadPicture(g_WSPC.szMDEVB_RootPath & "\bitmap\UnlockFields.bmp")

you might want to put ImageList control on your form, fill it with all pictures you will use. then
cmd_unlockfields.Picture = ImageList1.ListImages("unlock").Picture

That way your images will be loaded only once at start.
But if amount of pictures is too much your method is preferable.
very glaring problems in ur code!!! ;)

cant really think of many issues....
the main thing i notice is...lack of spacing and readability...
some spaces between the lines and sprinking of comments wud be gr8..

all the best for ur inteview ;)
why do u have:

Select Case szCmd
    Case IFC_CMD_FRMOPEN
        Call IFC_PARA_Get(hIFC, IFC_PARA_DIALNR, g_iDialnr)
    Case IFC_CMD_CTXMENU
    Case IFC_CMD_LANGUAGE
    Case IFC_CMD_OVERPICKING
    Case IFC_CMD_CMD_CMD
    Case IFC_CMD_CMD_MISC
End Select

when u are only coding for a single case?

i agree with the extensibility factor...but i dunno...less code...more readable ;)
avoid declarations like

dim x as integer
dim temp as string

more meaningful naming....

i guess there are places where we run out of names to call 'temporary' stuff...but i guess some names are req.
there seems to quite some code with does the same thing say locking or clearing contents for a fixed set of textboxes.  u cud do somethng like:

for each control in me.controls
if type(control) = "textbox" then
' do stuff
end if
next

not sure if this is appropriate for ur context...but then..

Where u have a lot of
me.this control
me.that control

u cud use:

with Me
   .this control
   .that control
end with

looks ok....you are selected...;)
Thanks for your posts so far folks.

The lack of 'oh my gawds' has given me a bit of confidence that nobody will bite me too hard during my presentation.

The:
Select Case szCmd
    Case IFC_CMD_FRMOPEN
        Call IFC_PARA_Get(hIFC, IFC_PARA_DIALNR, g_iDialnr)
    Case IFC_CMD_CTXMENU
    Case IFC_CMD_LANGUAGE
    Case IFC_CMD_OVERPICKING
    Case IFC_CMD_CMD_CMD
    Case IFC_CMD_CMD_MISC
End Select

Is more a defensive programming thing, when I setup a select I write in all possibles at that time so's not to forget anything later.

I think I will keep the 'Me' in the same way I keep the std:: when I code in C++:  I hate getting tripped up by subtle syntax issues.
ASKER CERTIFIED SOLUTION
Avatar of brianb99999
brianb99999

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi All

Again thanks for your contribution.  All posts were useful and after a grueller of an interview I'm glad it's all over.

I have allocated the points to Brian as his reply was organised and certainly something I will adopt straight away.

AJG