VERSION 5.00
Begin VB.Form frmParameters 
   Caption         =   "Parameters"
   ClientHeight    =   3810
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4365
   Icon            =   "frmParameters.frx":0000
   LinkTopic       =   "Form1"
   ScaleHeight     =   3810
   ScaleWidth      =   4365
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdOK 
      Caption         =   "&Ok "
      Height          =   325
      Left            =   1905
      TabIndex        =   8
      Top             =   3465
      Width           =   810
   End
   Begin VB.CommandButton cmdCancel 
      Cancel          =   -1  'True
      Caption         =   "&Cancel "
      Height          =   325
      Left            =   3540
      TabIndex        =   10
      Top             =   3465
      Width           =   810
   End
   Begin VB.CommandButton cmdDelete 
      Caption         =   "&Delete"
      Height          =   325
      Left            =   2715
      TabIndex        =   9
      Top             =   3465
      Width           =   810
   End
   Begin VB.Frame Frame1 
      Caption         =   "Parameters"
      Height          =   3375
      Left            =   45
      TabIndex        =   11
      Top             =   30
      Width           =   4305
      Begin VB.ComboBox cmbFunction 
         Height          =   315
         ItemData        =   "frmParameters.frx":000C
         Left            =   1530
         List            =   "frmParameters.frx":0016
         TabIndex        =   7
         Top             =   2940
         Width           =   2250
      End
      Begin VB.ComboBox cmbChannels 
         Height          =   315
         ItemData        =   "frmParameters.frx":0044
         Left            =   1530
         List            =   "frmParameters.frx":0057
         TabIndex        =   6
         Top             =   2595
         Width           =   2430
      End
      Begin VB.TextBox txtPollingTime 
         Height          =   300
         Left            =   1530
         TabIndex        =   5
         Top             =   2280
         Width           =   735
      End
      Begin VB.ComboBox cmbBaudRate 
         Height          =   315
         ItemData        =   "frmParameters.frx":006F
         Left            =   1530
         List            =   "frmParameters.frx":007F
         TabIndex        =   4
         Top             =   1950
         Width           =   2100
      End
      Begin VB.ComboBox cmbPort 
         Height          =   315
         ItemData        =   "frmParameters.frx":00AB
         Left            =   1530
         List            =   "frmParameters.frx":00B5
         TabIndex        =   3
         Top             =   1605
         Width           =   1230
      End
      Begin VB.ComboBox cmbParameter 
         Height          =   315
         ItemData        =   "frmParameters.frx":00C5
         Left            =   1530
         List            =   "frmParameters.frx":00D5
         TabIndex        =   2
         Top             =   1275
         Width           =   2415
      End
      Begin VB.TextBox txtDevID 
         Height          =   285
         Left            =   1530
         TabIndex        =   1
         Top             =   660
         Width           =   1620
      End
      Begin VB.ComboBox cmbDevice 
         Height          =   315
         Left            =   1530
         TabIndex        =   0
         Top             =   315
         Width           =   2490
      End
      Begin VB.Label Label18 
         Caption         =   "*"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H000000FF&
         Height          =   240
         Left            =   3975
         TabIndex        =   29
         Top             =   1230
         Width           =   135
      End
      Begin VB.Label Label17 
         Caption         =   "*"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H000000FF&
         Height          =   240
         Left            =   3810
         TabIndex        =   28
         Top             =   2895
         Width           =   135
      End
      Begin VB.Label Label16 
         Caption         =   "*"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H000000FF&
         Height          =   240
         Left            =   4005
         TabIndex        =   27
         Top             =   2550
         Width           =   135
      End
      Begin VB.Label Label15 
         BackStyle       =   0  'Transparent
         Caption         =   "*"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H000000FF&
         Height          =   240
         Left            =   2295
         TabIndex        =   26
         Top             =   2250
         Width           =   135
      End
      Begin VB.Label Label14 
         Caption         =   "*"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H000000FF&
         Height          =   240
         Left            =   3675
         TabIndex        =   25
         Top             =   1905
         Width           =   135
      End
      Begin VB.Label Label13 
         Caption         =   "*"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H000000FF&
         Height          =   240
         Left            =   2790
         TabIndex        =   24
         Top             =   1575
         Width           =   135
      End
      Begin VB.Label Label12 
         Caption         =   "*"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H000000FF&
         Height          =   240
         Left            =   3195
         TabIndex        =   23
         Top             =   630
         Width           =   135
      End
      Begin VB.Label Label10 
         Caption         =   "Function          :"
         Height          =   255
         Left            =   150
         TabIndex        =   22
         Top             =   2910
         Width           =   1305
      End
      Begin VB.Label Label9 
         Caption         =   "Channels         :"
         Height          =   255
         Left            =   150
         TabIndex        =   21
         Top             =   2565
         Width           =   1305
      End
      Begin VB.Label Label8 
         Caption         =   "Polling Time     :"
         Height          =   255
         Left            =   150
         TabIndex        =   20
         Top             =   2265
         Width           =   1305
      End
      Begin VB.Label Label7 
         Caption         =   "Baud Rate       :"
         Height          =   255
         Left            =   150
         TabIndex        =   19
         Top             =   1935
         Width           =   1305
      End
      Begin VB.Label Label6 
         Caption         =   "Port                  :"
         Height          =   255
         Left            =   150
         TabIndex        =   18
         Top             =   1620
         Width           =   1305
      End
      Begin VB.Label Label5 
         Caption         =   "Device ID         :"
         Height          =   255
         Left            =   150
         TabIndex        =   17
         Top             =   675
         Width           =   1305
      End
      Begin VB.Label Label4 
         Caption         =   "Parameter         :"
         Height          =   255
         Left            =   150
         TabIndex        =   16
         Top             =   1305
         Width           =   1305
      End
      Begin VB.Label Label2 
         Caption         =   " Status              :"
         Height          =   255
         Left            =   150
         TabIndex        =   15
         Top             =   990
         Width           =   1305
      End
      Begin VB.Label Label1 
         Caption         =   "Device Name    :"
         Height          =   255
         Left            =   150
         TabIndex        =   14
         Top             =   360
         Width           =   1305
      End
      Begin VB.Label Label11 
         Caption         =   "Sec."
         Height          =   270
         Left            =   2385
         TabIndex        =   13
         Top             =   2325
         Width           =   345
      End
      Begin VB.Label Label3 
         Caption         =   "Not Connected..."
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H8000000D&
         Height          =   270
         Left            =   1530
         TabIndex        =   12
         Top             =   975
         Width           =   1695
      End
   End
End
Attribute VB_Name = "frmParameters"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdCancel_Click()
 Unload Me
End Sub

Private Sub cmdOK_Click()
Dim con As New ADODB.Connection
Dim rec As New ADODB.Recordset
Dim qry As String

 '---chk for validations
 If Not fnValidate Then Exit Sub
 
 Set con = getConnection


 '---insert/update data in database
  If frmDisplay.lblSrno.Caption <> "" And Not IsEmpty(frmDisplay.lblSrno.Caption) Then
    qry = ""
    qry = " UPDATE modbus SET " & _
          " DEVICE_NAME = " & fnChkNull(cmbDevice.Text) & " , " & _
          " DEVICE_ID = " & fnChkNum(txtDevID.Text) & " , " & _
          " PARAMETER = " & fnChkNull(cmbParameter.Text) & " , " & _
          " COMM_PORT = " & fnChkNull(cmbPort.Text) & " , " & _
          " BAUD_RATE = " & fnChkNull(cmbBaudRate.Text) & " , " & _
          " POLLING_TIME = " & fnChkNum(txtPollingTime.Text) & " , " & _
          " CHANNELS = " & fnChkNum(cmbChannels.Text) & " , " & _
          " FUNCTION_ID = " & fnChkNull(cmbFunction.Text) & _
          " WHERE SRNO = " & Trim(frmDisplay.lblSrno.Caption)
  Else
    qry = ""
    qry = " INSERT INTO modbus ( DEVICE_NAME , DEVICE_ID, PARAMETER, COMM_PORT, BAUD_RATE, " & _
                               " POLLING_TIME, CHANNELS, FUNCTION_ID ) values ( " & _
           fnChkNull(cmbDevice.Text) & " , " & _
           fnChkNum(txtDevID.Text) & " , " & _
           fnChkNull(cmbParameter.Text) & " , " & _
           fnChkNull(cmbPort.Text) & " , " & _
           fnChkNull(cmbBaudRate.Text) & " , " & _
           fnChkNum(txtPollingTime.Text) & " , " & _
           fnChkNum(cmbChannels.Text) & " , " & _
           fnChkNull(cmbFunction.Text) & _
           ")"
  End If
  MsgBox "qry :: " & qry
  con.Execute qry
  
 '---unload the window
 Unload Me
End Sub

Private Sub Form_Load()
    
    '--set the dafualt values
    cmbDevice.Text = "Select/Enter Device Name"
    cmbParameter.Text = "Select /Enter the Parameter"
    cmbPort.Text = "Select Port"
    cmbBaudRate.Text = "Select/Enter Baud Rate"
    cmbChannels.Text = "Select/Enter No. of Channels"
    cmbFunction.Text = "Select /Enter the function"
    
    
    '---- making the Delete button enable / disable ...
    If frmDisplay.bNewFlag Then
        cmdDelete.Enabled = False
    Else
        cmdDelete.Enabled = True
    End If

  If frmDisplay.lblSrno.Caption <> "" And Not IsEmpty(frmDisplay.lblSrno.Caption) Then
    Dim rec As New ADODB.Recordset
    Dim con As New ADODB.Connection
    Dim qry As String
    Set con = modConn.getConnection
    qry = ""
    qry = " select * from modbus " & _
          " where srno = " & frmDisplay.lblSrno.Caption

    rec.Open qry, con, adOpenDynamic

    If Not rec.EOF Then
     cmbDevice.Text = rec.Fields("DEVICE_NAME") & ""
     txtDevID.Text = rec.Fields("DEVICE_ID") & ""
     cmbParameter.Text = rec.Fields("PARAMETER") & ""
     cmbPort.Text = rec.Fields("COMM_PORT") & ""
     cmbBaudRate.Text = rec.Fields("BAUD_RATE") & ""
     txtPollingTime.Text = rec.Fields("POLLING_TIME") & ""
     cmbChannels.Text = rec.Fields("CHANNELS") & ""
     If rec.Fields("FUNCTION_ID") = "4" Then
       cmbFunction.Text = "Read Registers(04)"
     Else
       cmbFunction.Text = "Write Registers(01)"
     End If
     
    End If
    rec.Close
  End If '---LBLSRNO.Caption <> ""

End Sub

Private Function fnValidate() As Boolean
 '--chk for validations
 'If Not chkCombo(cmbDevice, "Please select/enter the device name") Then fnValidate = False: Exit Function
 If Not chkText(txtDevID, "Please enter device ID") Then fnValidate = False: Exit Function
 
 '---chk whether devID is valid or not
 If Trim(txtDevID.Text) <> "" And Not IsNumeric(txtDevID.Text) Then
   MsgBox "Please enter a valid Device ID.", vbInformation, "Invalid Device ID..."
   txtDevID.SetFocus
   fnValidate = False
   Exit Function
 End If
 
 If Not chkCombo(cmbParameter, "Select /Enter the Parameter", "Please select the parameter") Then fnValidate = False: Exit Function
 If Not chkCombo(cmbPort, "Select Port", "Please select the port") Then fnValidate = False: Exit Function
 If Not chkCombo(cmbBaudRate, "Select/Enter Baud Rate", "Please select/enter the baud rate") Then fnValidate = False: Exit Function
 If Not chkText(txtPollingTime, "Please enter the polling time") Then fnValidate = False: Exit Function
 If Not chkCombo(cmbChannels, "Select/Enter No. of Channels", "Please select the channel") Then fnValidate = False: Exit Function
 If Not chkCombo(cmbFunction, "Select /Enter the function", "Please select/enter the function") Then fnValidate = False: Exit Function
 
 fnValidate = True
End Function


Private Function chkCombo(ByRef cmb As ComboBox, txt As String, ByRef Msg As String) As Boolean
Dim str As String
     '---chk for null value or selected index
    str = Trim(cmb.Text)
    If str = "" Or IsNull(str) Or IsEmpty(str) Or str = txt Then
     MsgBox Msg
     cmb.SetFocus
     chkCombo = False
     Exit Function
    End If
 
'---return true
chkCombo = True
End Function


Private Function chkText(ByRef txt As TextBox, ByRef Msg As String) As Boolean
 '---chk for null value or selected index
 If Trim(txt.Text) = "" Or IsNull(txt.Text) Then
  MsgBox Msg
  txt.SetFocus
  chkText = False
  Exit Function
 End If
 
'---return true
chkText = True

End Function

'---purpose : to replace single appostrophe with two sinle ones
Private Function chkApo(str As String)
Dim stTemp, stFinal As String
Dim i As Integer

    stFinal = ""
    For i = 1 To Len(str)
     stTemp = Mid(str, i, 1)
     If stTemp = "'" Then stTemp = "''"
     stFinal = stFinal + stTemp
    Next
     chkApo = stFinal
End Function


Private Function fnChkNull(str As String) As String
    str = Trim(str)
    If str = "" Or IsNull(str) Or IsEmpty(str) Then
        fnChkNull = "null"
    Else
        str = chkApo(str)
        fnChkNull = "'" & Trim(str) & "'"
    End If
End Function

