Page 1


INTRODUCCION:

Este es un manual de visual Basic 6.o en la cual puedo dar a conocer de lo que yo he emprendido en programación en el presente año y que espero que ustedes también puedan aprender algo sobre este manual de lo que he diseñado para ustedes.


FELX GRID

En el siguiente codigo fuente lo que hace es rescribir , guardar y abrir en felx grid

Controles: 5 textbox 4 label 1 MSHFlexGrid1 4 commandbuton 1commondialg 2 timer


Text1 Text1 Text1 Text1 Text1 Command1 Command2 Command3 Command4 MSHFlexGrid1

Text0 Text E Text Y Text1 txtEdit Guardar Abrir Limpiar Serrar factura

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit Private GrdDat As String Private GrdDat2 As String Private GrdDat3 As String Private ControlVisible As Boolean Private LastRow As Long Private LastCol As Long Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" ( _ ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long 'Flag Private Backspaced As Boolean ''''''''''''''''''''''''''''''''''''''' Private Sub Abrir_Click() Abrir.Show

End Sub '''''''''''''''''''''''''''''''''''''''''''''''' Private Sub ChameleonBtn1_Click() CommonDialog1.ShowOpen


CommonDialog2.ShowSave End Sub '''''''''''''''''''''''''''''''''''''''''''''' Private Sub guar() Dim Nmb As Long Dim Drc As Long Dim Ntt As Long Dim Tel As Long Nmb = FreeFile Drc = FreeFile Ntt = FreeFile Tel = FreeFile Open GrdDat For Output As Nmb & Drc & Ntt And Tel Print #Nmb, TextO.Text Print #Drc, Text1.Text Print #Ntt, TextY.Text Print #Tel, TextE.Text Close Nmb End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Guar2() Dim Flex As Long Dim r As Long Dim c As Long Flex = FreeFile Open GrdDat2 For Output As Flex For r = 1 To Fac.Rows - 1 For c = 1 To Fac.Cols - 1 Print #Flex, Fac.TextMatrix(r, c) Next Next Close Flex End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub cmdAbrir_Click() Abrir.Show End Sub ''''''''''''''''''''''''''''''''''''''''''' Private Sub cmdGuardar_Click() On Error GoTo error Dim a As String If Len(Dir$(GrdDat)) = 0 Then guar Guar2 a = Abrir.fg1.Rows - 1


Abrir.fg1.TextMatrix(a, 1) = TextO.Text Abrir.fg1.TextMatrix(a, 2) = TextY.Text Abrir.fg1.TextMatrix(a, 3) = Text1.Text Abrir.fg1.TextMatrix(a, 8) = TextE.Text Abrir.fg1.Rows = Abrir.fg1.Rows + 1 If MsgBox("Desea Guardar y Crear un nuevo Pedido", vbQuestion + vbYesNo, "Guardar") = vbYes Then TextO.Text = TextO.Text + 1 TextO.Text = TextO.Text 'Limpiar Else End If MsgBox "Los datos han sido guardados", vbInformation, "Guardar" Else MsgBox "El Archivo que desea crear ya Existe", vbCritical, "El Archivo ya Existe" End If error: MsgBox "Guardando", vbInformation, "Dato Guardado" End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Command2_Click() Fac.Clear Dim s As String Dim i As Long With Fac .Cols = 5 .FixedRows = 1 .FixedCols = 1 .ScrollBars = flexScrollBarBoth .AllowUserResizing = flexResizeColumns .TextArray(0) = "" .ColWidth(0) = 500 .TextArray(1) = "Cantidad" .ColWidth(1) = 2000 .TextArray(2) = "Descripcion" .ColWidth(2) = 2500 .TextArray(3) = "P/u" .ColWidth(3) = 2500 .TextArray(4) = "Monto" .ColWidth(4) = 2000


For i = 1 To .Rows - 1 .TextMatrix(i, 0) = i .RowHeight(0) = 500 Next End With TextO.Text = "" TextY.Text = "" TextE.Text = "" Text1.Text = ""

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Command3_Click() Dim a a = MsgBox("quiere usted abandonar el programa", vbQuestion + vbYesNo, "Salir") If a = vbYes Then factura.Hide End Else If a = vbYesNo Then factura.Show End If End If End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Form_Load() Dim s As String s = App.Path GrdDat = s & IIf(Right$(s, 1) = "\", "", "\") & ".dll" Fac.Rows = 30 With Fac .Cols = 5


.Rows = 19 .FixedRows = 1 .FixedCols = 1 .ScrollBars = flexScrollBarBoth .AllowUserResizing = flexResizeColumns .TextArray(0) = "" .ColWidth(0) = 500 .TextArray(1) = "Cantidad" .ColWidth(1) = 2000 .TextArray(2) = "Descripcion" .ColWidth(2) = 2500 .TextArray(3) = "P/u" .ColWidth(3) = 2500 .TextArray(4) = "Monto" .ColWidth(4) = 2000 Dim i For i = 1 To .Rows - 1 .TextMatrix(i, 0) = i .RowHeight(0) = 500 Next

End With

End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Fac_KeyUp(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyDelete Fac.Text = "" Case vbKeyBack If Len(Fac.Text) > 0 Then Fac.Text = Left(Fac.Text, Len(Fac.Text) - 1) End If End Select End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Fac_KeyPress(KeyAscii As Integer) If KeyAscii >= 32 And KeyAscii <= 127 Then Fac.Text = Fac.Text & Chr(KeyAscii) End If


End Sub ''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Image1_Click() End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Timer1_Timer() Text10.Text = Val(Fac.TextMatrix(1, 4)) + Val(Fac.TextMatrix(2, 4)) + Val(Fac.TextMatrix(3, 4)) + Val(Fac.TextMatrix(4, 4)) + Val(Fac.TextMatrix(5, 4)) + Val(Fac.TextMatrix(6, 4)) + Val(Fac.TextMatrix(7, 4)) + Val(Fac.TextMatrix(8, 4)) + Val(Fac.TextMatrix(9, 4)) Text10.Text = Format(Text10.Text, "#,###0.00##") Text10.Text = "Q. " & Text10.Text '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Fac.TextMatrix(Cnt.Caption, 4) = Val(Cant.Text) * Val(Prec.Text) Fac.TextMatrix(Cnt, 4) = Format(Fac.TextMatrix(Cnt, 4), "####0.00##") Fac.TextMatrix(1, 4) = Format(Fac.TextMatrix(1, 4), "####0.00##") Fac.TextMatrix(1, 4) = Val(T1.Text) * Val(T2.Text) Fac.TextMatrix(1, 4) = Format(Fac.TextMatrix(1, 4), "####0.00##") Cnt.Caption = Cnt.Caption + 1 Cnt.Caption = Cnt.Caption Cant.Text = Fac.TextMatrix(Cnt.Caption, 1) Prec.Text = Fac.TextMatrix(Cnt.Caption, 3)

T1.Text = Fac.TextMatrix(1, 1) T2.Text = Fac.TextMatrix(1, 3)

If Cnt.Caption = Fac.Rows - 1 Then Cnt.Caption = 1 End If End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Sub Timer2_Timer()


Dim s As String Dim Gr As String Gr = TextO.Text & "_NIT_" & TextY.Text s = App.Path GrdDat = s & IIf(Right$(s, 1) = "\", "", "\") & "\Datos\1\" & Gr & ".ICC" GrdDat2 = s & IIf(Right$(s, 1) = "\", "", "\") & "\Datos\" & Gr & ".ICC" End Sub

FORM ABRIR

1 MSHFlexGrid1 2 command 1 timer 1Commondialg


MSHFlexGrid1 Command1 Command2

Fg1 Recuperar Abrir23

Option Explicit Private GrdDat As String Private GrdDatD As String Private GrdDatP As String Private GrdDatP3 As String Private GrdDat2P As String Private GrdDat2 As String ''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Recupera_Click() AbrPedido AbrirFlex Abrir.Visible = False factura.Show factura.Enabled = True

End Sub ''''''''''''''''''''''''''''''''''''''''''''''' Private Sub AbrPedido() Dim Nmb As Long Dim Drc As Long Dim Ntt As Long Dim Tel As Long Dim q As String Dim w As String Dim e As String Dim r As String If Len(Dir$(GrdDatP)) = 0 Then Exit Sub ' Si no existe el fichero, nada que hacer... Nmb = FreeFile Drc = FreeFile Ntt = FreeFile Tel = FreeFile Open GrdDatP For Input As Nmb & Drc & Ntt And Tel Do While Not EOF(Nmb) Do While Not EOF(Drc)


Do While Not EOF(Ntt) Do While Not EOF(Tel)

If Not EOF(Nmb) Then Line Input #Nmb, q factura.TextO.Text = q If Not EOF(Drc) Then Line Input #Drc, w factura.Text1.Text = w If Not EOF(Ntt) Then Line Input #Ntt, e factura.TextY.Text = e If Not EOF(Tel) Then Line Input #Tel, r factura.TextE.Text = r Else End If End If End If End If Loop Loop Loop Loop Close Nmb End Sub '''''''''''''''''''''''''''''''''''''''''''' Private Sub AbrirFlex() Dim Flex As Long Dim r As Long Dim c As Long Dim q As String If Len(Dir$(GrdDat2)) = 0 Then Exit Sub 'r = frmPedido.Fg1.Rows - 9 Flex = FreeFile Open GrdDat2 For Input As Flex Do While Not EOF(Flex) 'r = r + 1 'frmPedido.Fg1.Rows = r + 1 'frmPedido.Fg1.TextMatrix(r, 0) = r For r = 1 To factura.Fac.Rows - 1 For c = 1 To factura.Fac.Cols - 1


If Not EOF(Flex) Then Line Input #Flex, q factura.Fac.TextMatrix(r, c) = q Else Exit For End If Next Next Loop Close Flex End Sub '''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Form_Load() Cargar2 Dim s As String Dim Gr As String Dim GrF As String GrF = fg1.RowSel Gr = fg1.TextMatrix(GrF, 1) & "_Serie_" & fg1.TextMatrix(GrF, 2) s = App.Path GrdDatD = s & IIf(Right$(s, 1) = "\", "", "\") & "\Datos\1\" & Gr & ".ICC" GrdDat2 = s & IIf(Right$(s, 1) = "\", "", "\") & "\Datos\" & Gr & ".ICC" Abrir1 End Sub Private Sub Cargar2() Dim i As Long With fg1 .Cols = 5 .FixedRows = 1 .FixedCols = 1 .ScrollBars = flexScrollBarBoth .AllowUserResizing = flexResizeColumns .TextArray(0) = "No." .ColWidth(0) = 500 .TextArray(1) = "Nombre" .ColWidth(1) = 3000 .TextArray(2) = "Nit" .ColWidth(2) = 2000 .TextArray(3) = "Direccion" .ColWidth(3) = 4000 .TextArray(4) = "Telefono" .ColWidth(4) = 2000


For i = 1 To .Rows - 1 .TextMatrix(i, 0) = i .RowHeight(0) = 500 Next End With End Sub '''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Abrir1() Dim Flex As Long Dim r As Long Dim c As Long Dim q As String If Len(Dir$(GrdDatD)) = 0 Then Exit Sub r = fg1.Rows - 2 Flex = FreeFile Open GrdDatD For Input As Flex Do While Not EOF(Flex) r=r+1 fg1.Rows = r + 1 fg1.TextMatrix(r, 0) = r For c = 1 To fg1.Cols - 1 If Not EOF(Flex) Then Line Input #Flex, q fg1.TextMatrix(r, c) = q Else Exit For End If Next Loop Close Flex End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Timer1_Timer() Dim s As String Dim Gr As String Dim GrF As String GrF = fg1.RowSel Gr = fg1.TextMatrix(GrF, 1) & "_NIT_" & fg1.TextMatrix(GrF, 2) s = App.Path

GrdDatP = s & IIf(Right$(s, 1) = "\", "", "\") & "\Datos\1\" & Gr & ".ICC"


GrdDat2 = s & IIf(Right$(s, 1) = "\", "", "\") & "\Datos\" & Gr & ".ICC" End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Exit_Click() Unload Me factura.Enabled = True End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Abrir23_Click() With CommonDialog1 .DialogTitle = " Busque el archivo que desea recuperar" .InitDir = App.Path & "\Datos\" .Filter = "Archivos DAT|*.ICC"

Timer1.Enabled = False Dim s As String Dim Ft As String .ShowOpen s = App.Path Ft = .FileTitle GrdDatP = s & IIf(Right$(s, 1) = "\", "", "\") & "Datos\1\" & Ft GrdDat2 = s & IIf(Right$(s, 1) = "\", "", "\") & "Datos\" & Ft If .FileName = "" Then Exit Sub Recupera_Click Timer1.Enabled = True End With End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Timer2_Timer() Label1.Caption = Time Label2.Caption = Date End Sub


DATA GRID

Este c贸digo ayuda a buscar alg煤n nombre o dato en el data

C贸digo fuente en el formulario Texto planoCopiar c贸digo fuenteImprimir 1. Option Explicit 2. 3. '****************************************************** *****************************


4. ' Ejemplo para buscar en un DataGrid con el m茅todo Find del recordset 5. 6. 'Controles : 1 - La referencia a Ado _ 7. 2 - Un control DataGrid ( DataGRid1 ) _ 8. 3 - Un control Textbox (Text1) _ 9. 4 Dos CommandButon ( Command1 y Command2 ) _ 10. 5 - Un Combobox ( Combo1 Para los campos ) _ 11. 6 - Un CheckBox _ 12. 7 Indicar en la constante s_CONNECTION_STRING la _ 13. cadena de conexi贸n para la base de datos _ 14. 8 Indicar en el FormLoad, la consulta Sql para el record set 15. 16. 17. 18. '************************************************** ********************************* 19. 20. '************************************************** ********************************* 21. 22. ' ConnectionString 23. Private Const s_CONNECTION_STRING As String = "Prov ider=Microsoft.Jet.OLEDB.4.0;" & _ 24. "Data Source=C:\Archivos de programa" & _ 25. "\Micr osoft Visual Studio\VB98\" & _ 26. "NWIND .MDB;Persist Security Info=False" 27. 28. ' Colores de fondo para los textbox mientras se bus ca 29. Private Const COLOR_TEXTBOX_NO_FOUND As Long = &H80 80FF 30. Private Const COLOR_TEXTBOX_FOUND As Long = &HC0FFF F 31. Private Const COLOR_TEXTBOX_NORMAL As Long = vbWhit e


32. '************************************************** ********************************* 33. 34. 35. ' Variable de tipo Recordset y con evento 36. Private WithEvents Recordset As ADODB.Recordset 37. 'Botón para buscar hacia atrás 38. Private Sub Command1_Click() 39. 40. ' Si llega al final posiciona el recordset en e l último registro 41. If Recordset.EOF Then 42. Recordset.MoveLast 43. End If 44. ' Habilita y deshabilita los command de buscar 45. If Recordset.BOF Or Recordset.AbsolutePosition <= 1 Then 46. Command1.Enabled = False 47. Command2.Enabled = True 48. Command2.SetFocus 49. Exit Sub 50. End If 51. 52. Dim Anterior As Long 53. ' guarda la posición del Registro anterior 54. Anterior = Recordset.AbsolutePosition 55. ' Mueve el cursor 56. Recordset.Move 0, Recordset.Bookmark - 1 57. 58. ' Busca en cualquier parte de la cadena 59. If Check1.Value = 0 Then 60. Recordset.Find Combo1.Text & " LIKE '*" + T ext1.Text + "*'", , adSearchBackward 61. ' Busca la cadena completa 62. ElseIf Check1.Value = 1 Then 63. Recordset.Find Combo1.Text & "='" + Text1.T ext + "'", , adSearchBackward 64. End If 65. 66. Command2.Enabled = True 67. ' Si llega al principio , selecciona la última fila encontrada 68. If Recordset.BOF Then 69. Recordset.AbsolutePosition = Anterior 70. Command1.Enabled = False


71. Command2.SetFocus 72. End If 73. 74. End Sub 75. 'Bot贸n para buscar hacia adelante 76. Private Sub Command2_Click() 77. ' Si llega al principio posiciona el recordset en el primer registro 78. If Recordset.BOF Then 79. Recordset.MoveFirst 80. End If 81. ' Habilita y deshabilita los botones para busca r 82. If Recordset.EOF Or Recordset.AbsolutePosition >= Recordset.RecordCount Then 83. Command2.Enabled = False 84. Command1.Enabled = True 85. Command1.SetFocus 86. Exit Sub 87. End If 88. 89. Dim Anterior As Long 90. ' Almacena la fila actual 91. Anterior = Recordset.AbsolutePosition 92. ' Mueve un registro hacia atr谩s 93. Recordset.Move 0, Recordset.Bookmark + 1 94. 95. ' Busca en cualquier parte de la cadena 96. If Check1.Value = 0 Then 97. Recordset.Find Combo1.Text & " LIKE '*" + T ext1.Text + "*'", , adSearchForward 98. ' Busca la cadena completa 99. ElseIf Check1.Value = 1 Then 100. Recordset.Find Combo1.Text & "='" + Text1.T ext + "'", , adSearchForward 101. End If 102. 103. Command1.Enabled = True 104. 105. If Recordset.EOF Then 106. Command2.Enabled = False 107. Command1.SetFocus 108. Recordset.AbsolutePosition = Anterior 109. End If 110. 111. End Sub


112. 113. 114. Private Sub Form_Load() 115. 116. ' Variable para la conexión de Ado 117. Dim db As ADODB.Connection 118. 119. ' Nuevo objeto Connection 120. Set db = New ADODB.Connection 121. 122. db.CursorLocation = adUseClient 123. 124. ' Abre la base de datos pasando la cadena de co nexión 125. db.Open s_CONNECTION_STRING 126. 127. ' crea un Recordset 128. Set Recordset = New Recordset 129. 130. ' Abre el Recordset con la consulta Sql 131. Recordset.Open "Select [NombreContacto]," & _ 132.

"[NombreCompañía]," & _

133. "[CargoContacto] " & _ 134. "From Proveedores Order B y NombreContacto Asc", _ 135. db, adOpenStatic, adLockO ptimistic 136. 137. Dim i As Integer 138. 139. 'Recorre los campos del recordset para añadirlos al combobox 140. 'Nota: los campos deben ser de tipo String 141. For i = 0 To Recordset.Fields.Count - 1 142. Combo1.AddItem Recordset.Fields.Item(i).Nam e 143. Next i 144. 145. ' Selecciona el campo 1 146. Combo1.ListIndex = 0 147. 148. 149. ' Engancha el recordset al datagrid 150. Set DataGrid1.DataSource = Recordset


151. 152. ' Opcional . esto hace que se seleccione la fil a completa en el DataGrid 153. DataGrid1.MarqueeStyle = dbgHighlightRowRaiseCe ll 154. 155. Text1 = "" 156. 'caption de los controles 157. Command1.Caption = "Quitar Filtro" 158. Command1.Caption = " << Buscar Anterior " 159. Command2.Caption = " Buscar Siguiente >> " 160. Check1.Caption = " Buscar palabra completa" 161. 162. End Sub 163. 164. 165. Private Sub Text1_Change() 166. 167. If Text1 <> "" Then 168. 169. ' Habilita los botones para buscar hacia at r谩s y hacia adelante 170. Command1.Enabled = True 171. Command2.Enabled = True 172. 173. Dim Actual As Long 174. ' almacena la fila actual, por si no so se encontr贸 vuelve a posicionar _ 175. el recordset en dicha fila 176. If Not Recordset.EOF And Not Recordset.BOF Then 177. Actual = Recordset.AbsolutePosition 178. End If 179. 180. ' Busca en cualquier parte de la cadena 181. If Check1.Value = 0 Then 182. Recordset.Find Combo1.Text & " LIKE '*" & Text1.Text & "*'", , adSearchForward 183. ' Busca la Cadena completa 184. ElseIf Check1.Value = 1 Then 185. Recordset.Find Combo1.Text & "='" & Tex t1.Text & "'", , adSearchForward 186. End If 187. 188. ' Color del fondo del textbox


189. Then 190.

If Not Recordset.EOF And Not Recordset.BOF Text1.BackColor = COLOR_TEXTBOX_FOUND

191. Else 192. Text1.BackColor = COLOR_TEXTBOX_NO_FOUN D 193. End If 194. 195. If Recordset.BOF Or Recordset.EOF Then 196. Recordset.AbsolutePosition = Actual 197. End If 198. ' Si el textbox está vacío, posiciona el record set en el primer registro 199. Else 200. 201. Command1.Enabled = False 202. Command2.Enabled = False 203. 204. Recordset.MoveFirst 205. 206. Set DataGrid1.DataSource = Recordset 207. ' Color de fondo del textbox cuando está va cío 208. Text1.BackColor = COLOR_TEXTBOX_NORMAL 209. 210. End If 211. 212. End Sub 213. 214. ' Evento que se dispara cuando se cambia de posició n en el recordset 215. Private Sub Recordset_MoveComplete(ByVal adReason A s ADODB.EventReasonEnum, _ 216. ByVal pError As ADO DB.Error, adStatus As ADODB.EventStatusEnum, _ 217. ByVal pRecordset As ADODB.Recordset) 218. 219. ' Muestra en el caption del formulario el número registro actual 220. Me.Caption = " Registro actual: " & CStr(Recordse t.AbsolutePosition) 221. 222. End Sub 223.


224. ' Cuando se produce un error en el recordset se dis para este evento 225. Private Sub Recordset_Error(ByVal ErrorNumber As Lo ng, Description As String, _ 226. ByVal Scode As Long, ByVal Source As String, _ 227. ByVal HelpFile As String, ByVal HelpContext As Long, _ 228. fCancelDisplay As Boolean) 229. ' Mostramos el error 230. MsgBox " Descripci贸n del Error :" & Description, vbCritical 231. End Sub 232. 233. ' Cierra el recordset y Descarga la referencia 234. Private Sub Form_Unload(Cancel As Integer) 235. 236. ' Cierra 237. If Recordset.State = adStateOpen Then 238. Recordset.Close 239. End If 240. 241. ' descarga 242. If Not Recordset Is Nothing Then 243. Set Recordset = Nothing 244. End If 245. End Sub

manual de programacion  

contiene pasos para guardar datos