Results 1 to 8 of 8

Thread: To be modified to best & easy code:

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    To be modified to best & easy code:

    Hi friends,

    Could somebody make this code best & easy to understand. Do we really need the module for this project?
    VB Code:
    1. Public db As New ADODB.Connection
    2. Public rs As New ADODB.Recordset
    3. Public key As String
    4. Public deptID As Integer
    5.  
    6. Public Sub data_connect()
    7. Dim path As String
    8. path = App.path & "\db40.mdb"
    9. db.CursorLocation = adUseClient
    10. db.Open "Provider=microsoft.jet.oledb.4.0;persist security info=false;data source=" & path & ";"
    11. End Sub
    12.  
    13. Private Sub cmdadd_Click()
    14. If cmdadd.Caption = "&Add" Then
    15.     data_disable
    16.     txt_unlock
    17.     clear_txt
    18.     cmdsave.Enabled = True
    19.     cmdadd.Caption = "&Cancel"
    20.     cmdedit.Enabled = False
    21.     rs.MoveLast
    22.     deptID = rs(0)
    23.     deptID = deptID + 1
    24.     cmddel.Enabled = False
    25. Else
    26.     data_enable
    27.     txt_unlock
    28.     clear_txt
    29.     cmdsave.Enabled = False
    30.     cmdadd.Caption = "&Add"
    31.     cmdedit.Enabled = True
    32.     cmddel.Enabled = True
    33.     rs.Requery
    34.     show_rec
    35. End If
    36. End Sub
    37.  
    38. Private Sub data_disable()
    39. cmdprev.Enabled = False
    40. cmdfirst.Enabled = False
    41. cmdnext.Enabled = False
    42. cmdlast.Enabled = False
    43. End Sub
    44. Private Sub data_disable()
    45. cmdprev.Enabled = False
    46. cmdfirst.Enabled = False
    47. cmdnext.Enabled = False
    48. cmdlast.Enabled = False
    49. End Sub
    50.  
    51. Private Sub data_enable()
    52. cmdprev.Enabled = True
    53. cmdfirst.Enabled = True
    54. cmdnext.Enabled = True
    55. cmdlast.Enabled = True
    56. End Sub
    57.  
    58. Private Sub cmddel_Click()
    59. Dim ano As String
    60. ano = MsgBox("Do you really want to delete this record?", vbYesNo, "Comfirm Delete?")
    61. If ano = vbYes Then
    62.     db.Execute "Delete from dept where cusnum like " & rs(0)
    63.     rs.Requery
    64.     show_rec
    65. End If
    66. End Sub
    67.  
    68. Private Sub cmdedit_Click()
    69. data_disable
    70. txt_unlock
    71. key = Text1
    72. cmdsave.Enabled = True
    73. cmdedit.Enabled = False
    74. cmdadd.Enabled = False
    75. cmddel.Enabled = False
    76. End Sub
    77.  
    78. Private Sub cmdexit_Click()
    79. Unload Me
    80. End Sub
    81.  
    82. Private Sub cmdfirst_Click()
    83. On Error GoTo k
    84. rs.MoveFirst
    85. show_rec
    86. Exit Sub 'you need this or the error trapping code
    87.          'will ALWAYS execute
    88. k:
    89. If Err.Number = 3021 Then
    90. End If
    91. End Sub
    92.  
    93. Private Sub cmdlast_Click()
    94. On Error GoTo last
    95. rs.MoveLast
    96. show_rec
    97. Exit Sub 'you need this or the error trapping code
    98.          'will ALWAYS execute
    99. last:
    100. If Err.Number = 3021 Then
    101. End If
    102. End Sub
    103.  
    104. Private Sub cmdnext_Click()
    105. On Error GoTo cANTmOVEnEXT
    106. rs.MoveNext
    107. If rs.EOF Then
    108.     rs.MoveLast
    109. End If
    110. show_rec
    111. Exit Sub 'you need this or the error trapping code
    112.          'will ALWAYS execute
    113. cANTmOVEnEXT:
    114. If Err.Number = 3021 Then
    115. End If
    116. End Sub
    117.  
    118. Private Sub cmdprev_Click()
    119. On Error GoTo p
    120. rs.MovePrevious
    121. If rs.BOF Then
    122.     rs.MoveFirst
    123. End If
    124. show_rec
    125. Exit Sub 'you need this or the error trapping code
    126.          'will ALWAYS execute
    127. p:
    128. If Err.Number = 3021 Then
    129. End If
    130. End Sub
    131.  
    132. Private Sub cmdsave_Click()
    133. If cmdadd.Caption <> "&Cancel" Then
    134.     txt_lock
    135.     db.Execute "Update dept set Fname = '" & Text2 & "', Lname = '" & Text3 & "', Address = '" & Text4 & "', phonenum = '" & Text6 _
    136.     & "', credlimit = '" & Text8 & "', emaddress = '" & text5 & "', deptname = '" & Text7 & "' where cusnum = " & key
    137.     rs.Requery (1)
    138.     cmdsave.Enabled = False
    139.     cmdedit.Enabled = True
    140.     cmdadd.Enabled = True
    141. Else
    142.     txt_lock
    143.     db.Execute "Insert Into dept values (" & deptID & ",'" & Text2 & "','" & Text3 & "','" & Text4 & "','" & text5 & "','" & Text6 & "','" & Text7 & "','" & Text8 & "',0 ,0 )"
    144.     rs.Requery (1)
    145.     cmdsave.Enabled = False
    146.     cmdedit.Enabled = True
    147.     cmdadd.Enabled = True
    148.     cmdadd.Caption = "Add"
    149. End If
    150. data_enable
    151.  
    152. DataGrid1.Refresh
    153. cmddel.Enabled = True
    154. End Sub
    155.  
    156. Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
    157. On Error GoTo k
    158. show_rec
    159. Exit Sub 'you need this or the error trapping code
    160.          'will ALWAYS execute
    161. k:
    162. If Err.Number = 6160 Then Exit Sub
    163. End Sub
    164.  
    165. Private Sub fname_Click()
    166. Call Text9_Change
    167. Text9.SetFocus
    168. End Sub
    169.  
    170. Private Sub Form_Load()
    171. Call data_connect
    172. rs.Open "Select * from dept", db
    173. Set DataGrid1.DataSource = rs
    174. show_rec
    175. End Sub
    176. Public Sub txt_lock()
    177. Text1.Locked = True
    178. Text2.Locked = True
    179. Text3.Locked = True
    180. Text4.Locked = True
    181. text5.Locked = True
    182. Text6.Locked = True
    183. Text7.Locked = True
    184. Text8.Locked = True
    185. End Sub
    186.  
    187. Public Sub txt_unlock()
    188. Text1.Locked = False
    189. Text2.Locked = False
    190. Text3.Locked = False
    191. Text4.Locked = False
    192. text5.Locked = False
    193. Text6.Locked = False
    194. Text7.Locked = False
    195. Text8.Locked = False
    196. End Sub
    197.  
    198. Public Sub show_rec()
    199. Text1 = rs(0)
    200. Text2 = rs(1)
    201. Text3 = rs(2)
    202. Text4 = rs(3)
    203. text5 = rs(4)
    204. Text6 = rs(5)
    205. Text7 = rs(7)
    206. Text8 = rs(6)
    207. End Sub
    208.  
    209. Public Sub clear_txt()
    210. Text1 = ""
    211. Text2 = ""
    212. Text3 = ""
    213. Text4 = ""
    214. text5 = ""
    215. Text6 = ""
    216. Text7 = ""
    217. Text8 = ""
    218. End Sub
    219.  
    220. Private Sub Form_Unload(Cancel As Integer)
    221. Cancel = 1
    222. End Sub
    223.  
    224. Private Sub lname_Click()
    225. Call Text9_Change
    226. Text9.SetFocus
    227. End Sub
    228.  
    229. Private Sub text5_Change()
    230. If rs.RecordCount <> 0 Then
    231.     ctrl_unlock
    232. Else
    233.     ctrl_lock
    234. End If
    235. End Sub
    236. Public Sub ctrl_lock()
    237. data_disable
    238. cmdadd.Enabled = False
    239. cmdedit.Enabled = False
    240. cmddel.Enabled = False
    241. cmdexit.Enabled = False
    242. End Sub
    243.  
    244. Public Sub ctrl_unlock()
    245. data_enable
    246. cmdadd.Enabled = True
    247. cmdedit.Enabled = True
    248. cmddel.Enabled = True
    249. cmdexit.Enabled = True
    250. End Sub
    251.  
    252. Private Sub Text9_Change()
    253. rs.Close
    254. If fname.Value = True Then
    255.     rs.Open "Select * from dept where fname like '" & Text9 & "%'", db
    256.     rs.Requery
    257. Else
    258.     rs.Open "Select * from dept where lname like '" & Text9 & "%'", db
    259.     rs.Requery
    260. End If
    261.  
    262.  
    263. If rs.RecordCount <> 0 Then
    264.     show_rec
    265. Else
    266.     clear_txt
    267. End If
    268. End Sub
    Please clarify some doubts regarding the above project code:

    1. do we really need to put the connection in a module?
    2. why we need this txt5_change event?
    3. why do we need the ID field in this project? If we want to remove it what changes we have to do in the code?
    4.what is rs.query & rs.query(1)
    5. how it can be easily understable for the beginners?

    Could somebody modify this project code plz.





    Edit: Added [vbcode][/vbcode] tags and indenting for clarity. - Hack
    Attached Images Attached Images  
    Last edited by Hack; Oct 19th, 2005 at 06:17 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width