Tổng hợp Code VB 6.0



Code đọc tệp :
Private Sub cmddoc_Click()
Dim c As String
Dim b As String
// Code design by Seo Anh
c = InputBox$(" nhap file can doc")
lbl2.Caption = " cac phan tu co trong tap " + c + " la:  "
Open c For Input As 2
Do Until EOF(2)


Input #2, b
lbl2.Caption = lbl2.Caption + b + "   "
Loop
Close #2

End Sub

GHI TỆP CHẮN :

Private Sub cmdchan_Click()
Dim c As String
c = InputBox(" nhap file can ghi")
Open c For Output As 1
For i = 0 To n - 1
If a(i) Mod 2 = 0 Then
Write #1, a(i)
End If
Next
Close #1



End Sub

CHÈN TỆP:

Private Sub cmdchen_Click()
Dim x As Integer
Dim y As String
Dim tg As String

x = InputBox(" nhap vi tri can chen")
y = InputBox(" nhap phan tu can chen")
lbl2.Caption = " mang sau khi chen:  "
If x > n Then
MsgBox (" vi tri ban yeu cau lon hon do dai cua mang")
Else
For i = x To n
tg = a(i)
a(i) = y
y = tg
Next
End If
For i = 0 To n
lbl2.Caption = lbl2.Caption & a(i) & "  "
Next
End Sub

GHI TỆP LẺ:
Private Sub cmdle_Click()
Dim c As String
c = InputBox(" nhap file can ghi")
Open c For Output As 1
For i = 0 To n - 1
If a(i) Mod 2 <> 0 Then
Write #1, a(i)
End If
Next
Close #1
End Sub

SAO CHÉP TỆP:
Private Sub cmdsaochep_Click()
Dim c As String
Dim p As String
Dim x As String
Dim y As String

c = InputBox(" nhap file can coppy")
p = InputBox(" nhap file can pase")
Open p For Output As 1
Open c For Input As 2
Do Until EOF(2)

Input #2, x
y = y + "  " + x
Loop
Write #1, y


Close #1
Close #2

End Sub

NHẬP MẢNG :
Private Sub cmdnhap_Click()
n = CInt(txt)
lbl1.Caption = "day vua nhap la:  "
ReDim a(n)
For i = 0 To n - 1
a(i) = InputBox(" moi ban nhap phan tu thu" & i + 1)
lbl1.Caption = lbl1.Caption + a(i) + "  "
Next

End Sub

CHÈN 1 PHẦN TỬ VÀO MẢNG

Private Sub cmdchen_Click()
Dim x As Integer
Dim y As Integer
x = InputBox("Nhap vao vi tri can chen !")
y = InputBox("Nhap vao so can chen !")
If x > n Then
MsgBox ("Vi tri ban vua nhap k hop le !")
Else
For i = x To n
tg = a(i)
a(i) = y
y = tg
Next
For i = 0 To n
lblchen.Caption = lblchen.Caption & a(i) & "  "
Next
End If
End Sub

CODE IN RA DAY SO HOAN HAO:

Private Sub cmdhh_Click()
Dim dem As Integer
dem = 0
For i = 0 To n - 1
    For j = 1 To a(i) - 1
    If a(i) Mod j = 0 Then
    dem = dem + j
    End If
    Next
    If dem = a(i) Then
    dem = 0
    lblhienthi.Caption = lblhienthi.Caption & a(i) & "  "
    End If
    Next
End Sub


TRUNG BÌNH CỘNG CỦA MẢNG:
Private Sub cmdtbc_Click()
Dim s As Single
Dim c As Single
Dim tbc As Single
For i = 0 To n - 1
s = s + a(i)
c = c + 1
Next
tbc = s / c
lblhienthi.Caption = " TBC cua day so la:" & tbc
End Sub


KẾT NỐI CSDL : ADO


Private Sub cmdnhap_Click()
Me.Adodc1.Recordset.AddNew
Me.Adodc1.Recordset.Fields("macb") = txtma.Text
Me.Adodc1.Recordset.Fields("hoten") = txtten.Text
Me.Adodc1.Recordset.Fields("ngaysinh") = txtngaysinh.Text
Me.Adodc1.Recordset.Update
txtma.Text = ""
txtten.Text = ""
txtngaysinh.Text = ""
End Sub

Private Sub cmdsua_Click()
Me.Adodc1.Recordset.Fields("macb") = txtma.Text
Me.Adodc1.Recordset.Fields("hoten") = txtten.Text
Me.Adodc1.Recordset.Fields("ngaysinh") = txtngaysinh.Text
Me.Adodc1.Recordset.Update

End Sub

Private Sub cmdxoa_Click()
Dim ts As Integer
tl = MsgBox("Ban co thuc su muon xoa ?", vbYesNo)
If tl = vbYes Then
Me.Adodc1.Recordset.Delete
End If
End Sub

Private Sub DataGrid1_Click()
txtma.Text = Me.Adodc1.Recordset.Fields("macb")
txtten.Text = Me.Adodc1.Recordset.Fields("hoten")
txtngaysinh.Text = Me.Adodc1.Recordset.Fields("ngaysinh")
End Sub

CODE TÌM KIẾM TÊN THEO NHẬP VÀO :

Private Sub cmdtim_Click()
Dim sql As String
sql = "select * from sv where hoten like '" & (txtten.Text) & "'"
Me.Adodc1.RecordSource = sql
Me.Adodc1.Refresh
Me.DataGrid1.Refresh
End Sub


CODE NHÂN 2 MA TRẬN :
Private Sub CMDNHAN()

If h1 = c2 Then
ReDim tich(c1, h2)
For i = 1 To c1
For j = 1 To h2
For t = 1 To h1
tich(i, j) = tich(i, j) + (a(i, t) * b(t, j))
Next
lblht.Caption = lblht.Caption & tich(i, j) & “
Next
lblht.Caption = lblht.Caption & Chr(13)
Next
Else
lblht.Caption = " ko the nhan"
End If
End Sub

CODE TỔNG ĐƯỜNG CHÉO CHÍNH:

Private Sub cmdtongcheochinh_Click()
Dim tong As Integer

tong = 0
For i = 0 To m - 1
    For j = 0 To n - 1
        If i = j Then
            tong = tong + a(i, j)
        End If
      Next
Next
lblduongcheo.Caption = tong
End Sub
CODE TỔNG 2 MA TRẬN :

Private Sub cmdtong()
If (c1 <> c2 Or h1 <> h2) Then
lblht.Caption = "ko the cong)"
Else
ReDim tong(c1, h1)
For i = 1 To c1
For j = 1 To h1
tong(i, j) = a(i, j) + b(i, j)
lblht.Caption = lblht.Caption & tong(i, j)
Next
lblht.Caption = lblht.Caption & Chr(13)
Next
End If
End Sub


CODE TỔNG HÀNG CỦA MA TRẬN :

Private Sub Command1_Click()
For i = 0 To m - 1
    For j = 0 To n - 1
       
        lbla.Caption = lbla.Caption & " " & a(i, j)
    Next
        lbla.Caption = lbla.Caption & Chr(13)
Next

Dim tong As Integer

lblhienthi = "tong hang ma tran a la:"
For i = 0 To m - 1
    For j = 0 To n - 1
        tong = tong + a(i, j)
    Next
    lblhienthi.Caption = lblhienthi.Caption + "tong hang" & i & ":" & tong & Chr(13)
    tong = 0
Next

End Sub



Nhãn bài viết: |
Blog, Updated at: 00:08