Thứ ba, 22 Tháng tư 2003, 08:59 GMT+7

Cách nhập dữ liệu từ VB vào Access (1)

Toi muon lap trinh bang VB de nhap du lieu cho mot table trong Access. Cu the la toi co lam mot form co chua cac textbox de nhap du lieu, xin cac anh chi cho huong dan lam the nao de dua du lieu trong cac textbox do vao mot table trong Access theo cac truong tuong ung. Mong cac anh chi hoi am som. Cam on rat nhieu!

Nguyen Dai An

Hồi âm:

Em men!
Anh that su van chua hieu ro ve cau hoi cua em dua ra. Em nhap du lieu gi vay? Anh da viet thu tu tung phan lien ket giua lap trinh VB va Database de chi dan cach thuc duoi day cho em tham khao them. Vay em lam theo cach huong dan duoi day:

1. Cau tao mot ASCII text file de dung trong thi du nay. Neu em chua co cau tao san the text file, em co the bo qua buoc thu 5 -- Ngoai tru loading Grid1 with data from your text file.
2.Tang them three command buttons and two grid controls (GRID.VBX) to Form1.
3. Dung theo bang chi dan duoi day, dat the properties of the controls ma em da tang them o buoc thu 2.
Control Property New Value
--------------------------------------------------------------------
Command1 Caption "Press to Build Text File and Display in Grid"
Command2 Caption "Press to Transfer Data and Build New DB"
Command3 Caption "Press to Display the Data of the New Database"
Grid1 Cols 5
Grid1 Rows 35
Grid2 Cols 5
Grid2 Rows 35
4.Tang them code toi the (general) section of Form1:
Dim nums(30) As Long
Dim names(30) As String * 20
Dim addresses(30) As String * 25
Dim ss_nums(30) As String * 12
Const DB_LONG = 4
Const DB_TEXT = 10
Const DB_LANG_GENERAL = ";LANGID=0x0809;CP=1252;COUNTRY=0"
5.Tang them code toi the Form load event procedure:
Sub Form_Load ()
Show
grid1.ColWidth(1) = 1000 "For Emp ID
grid1.ColWidth(2) = 2000 "For Emp Name
grid1.ColWidth(3) = 3000 "For Emp Addr
grid1.ColWidth(4) = 2000 "For Emp SSN
grid1.Col = 1
grid1.Row = 0
grid1.Text = "Emp ID"
grid1.Col = 2
grid1.Row = 0
grid1.Text = "Emp Name"
grid1.Col = 3
grid1.Row = 0
grid1.Text = "Emp Addr"
grid1.Col = 4
grid1.Row = 0
grid1.Text = "Emp SSN" grid2.ColWidth(1) = 1000
grid2.ColWidth(2) = 2000
grid2.ColWidth(3) = 3000
grid2.ColWidth(4) = 2000
grid2.Col = 1
grid2.Row = 0
grid2.Text = "Employee ID"
grid2.Col = 2
grid2.Row = 0
grid2.Text = "Employee Name"
grid2.Col = 3
grid2.Row = 0
grid2.Text = "Employee Addr"
grid2.Col = 4
grid2.Row = 0
grid2.Text = "Employee SSN"
End Sub
6.Tang them code toi the Command1 click event procedure:
Sub Command1_Click ()
For i% = 1 To 30
nums(i%) = i%
names(i%) = "John Doe # " + Str$(i%)
addresses(i%) = Str$(i%) + " Mocking Bird Lane"
If i% < 9 Then
"* Enter the following four lines as one, single line:
ss_nums(i%) = Trim$(Str$(i%) + Trim$(Str$(i%))
+ Trim$(Str$(i%)) + "-" + Trim$(Str$(i% + 1))
+ Trim$(Str$(i% + 1)) + "-" + Trim$(Str$(i%))
+ Trim$(Str$(i%)) + Trim$(Str$(i%)) + Trim$(Str$(i%)))
Else
"* Enter the following two lines as one, single line:
ss_nums(i%) = Trim$(Trim$(Str$(999)) + "-" + Trim$(Str$(88))
+ "-" + Trim$(Str$(7777)))
End If
Next i%
Open "Testdata.DAT" For Output As #1
For j% = 1 To 30
Print #1, nums(j%)
Print #1, names(j%)
Print #1, addresses(j%)
Print #1, ss_nums(j%)
Next j%
Close #1
For i% = 1 To 30
grid1.Col = 1
grid1.Row = i%
grid1.Text = nums(i%) "Load Emp IDs
grid1.Col = 2
grid1.Row = i%
grid1.Text = names(i%) "Load Emp Names
grid1.Col = 3
grid1.Row = i%
grid1.Text = addresses(i%) "Load Emp Addrs
grid1.Col = 4
grid1.Row = i%
grid1.Text = ss_nums(i%) "Load Emp SSNs
Next i%
End Sub
7.Tang them code toi the Command2 click event procedure:
Sub Command2_Click ()
Dim newdb As Database
Dim newtb As Table
Dim newtd As New tabledef
Dim newidx As New Index
Dim field1 As New field "For Emp nums
Dim field2 As New field "For Emp names
Dim field3 As New field "For Emp addresses
Dim field4 As New field "For Emp ss_nums
screen.MousePointer = 11 "Display the time to build
Set newdb = CreateDatabase("NEWDB.MDB", DB_LANG_GENERAL)
newtd.Name = "Emp_Table" "* New table name
field1.Name = "Emp_ID" "* Holds Employee ID nums()
field1.Type = DB_LONG
newtd.Fields.Append field1
field2.Name = "Emp_Name" "* Holds Emp names()
field2.Type = DB_TEXT
field2.Size = 20
newtd.Fields.Append field2
field3.Name = "Emp_Addr" "* Holds Employee addr()
field3.Type = DB_TEXT
field3.Size = 25
newtd.Fields.Append field3
field4.Name = "Emp_SSN" "* Holds emp ss_nums()
field4.Type = DB_TEXT
field4.Size = 12
newtd.Fields.Append field4
newidx.Name = "Emp_ID_IDX" "* You have to have an index
newidx.Fields = "Emp_ID"
newidx.Primary = True
newtd.Indexes.Append newidx
newdb.TableDefs.Append newtd
Set newtb = newdb.OpenTable("Emp_Table")
Open "Testdata.dat" For Input As #1
BeginTrans
Do While Not (EOF(1))
newtb.AddNew
Line Input #1, tmp1$ "Retrieve empl_id
Line Input #1, tmp2$ "Retrieve empl_name
Line Input #1, tmp3$ "Retrieve empl_addr
Line Input #1, tmp4$
newtb("Emp_ID") = Trim$(tmp1$) "Place in field1
newtb("Emp_Name") = Trim$(tmp2$) "Place in field2
newtb("Emp_Addr") = Trim$(tmp3$) "Place in field3
newtb("Emp_SSN") = Trim$(tmp4$) "Place in field4
newtb.Update "Save to table
Loop
CommitTrans
Close #1 "Close text file
newtb.Close "Close DB"s table
newdb.Close "Close DB
screen.MousePointer = 0 "Set back to show done
End Sub
8.Tang them code toi the Command3 click event procedure:
Sub Command3_Click ()
Dim db As Database
Dim t As Table
Dim counter%
Set db = OpenDatabase("NEWDB.MDB")
Set t = db.OpenTable("Emp_Table")
counter% = 1 "Start counter at Row=1
Do Until t.EOF
grid2.Col = 1
grid2.Row = counter%
grid2.Text = t(0) "Load Emp ID
grid2.Col = 2
grid2.Row = counter%
grid2.Text = t(1) "Load Emp Name
grid2.Col = 3
grid2.Row = counter%
grid2.Text = t(2) "Load Emp Addr
grid2.Col = 4
grid2.Row = counter%
grid2.Text = t(3) "Load Emp SSN
counter% = counter% + 1
t.MoveNext
Loop
t.Close
db.Close
End Sub
9.Tu the Run menu, em chon Start (ALT, R, S), hoac nhan phim F5 to run chuong trinh nay. Dau tien em nhan vao nut the Command1. Sau do nhan vao nut the Command2 button, va roi nhan vao nut the Command3 button de so sanh cac ket qua. Chuc em vui ve!

Phucusa

Việt Báo

Nhận xét tin Cách nhập dữ liệu từ VB vào Access (1)

Ý kiến bạn đọc

Viết phản hồi

Bạn có thể gửi nhận xét, góp ý hay liên hệ về bài viết Cách nhập dữ liệu từ VB vào Access (1) bằng cách gửi thư điện tử tới Lien He Bao Viet Nam. Xin bao gồm tên bài viết Cach nhap du lieu tu VB vao Access 1 ở dạng tiếng Việt không dấu. Hoặc Cách nhập dữ liệu từ VB vào Access (1) ở dạng có dấu. Bài viết trong chuyên đề Kinh Nghiệm của chuyên mục Công Nghệ.

How to import data from Access VB (1)
As New, For Emp, Line Input, End Sub, Const DB, As String, data entry, 1, Trim, The, dim, de

I want programming in VB to import data to a table in Access. Specifically I have a form that contains a textbox to enter data, for all his expenses for guidance on how to put data in the textbox into a table in Access by ...



  • Thoi su 24h Nick Vujicic tai su kien Nick chao Viet Nam
    Thời sự 24h: Nick Vujicic tại sự kiện "Nick chào Việt Nam"

    EVN thông báo về sự cố mất điện toàn miền Nam; Sốc vì thu nhập "bèo" của các mại dâm nam; Trung Quốc "già mồm" đòi chủ quyền Trường Sa của Việt Nam; Xuất hiện "bút bi thần kì" lợi hại hơn "bút phù thủy"; Phố “trai gọi” chuyên phục vụ quý bà; "Rẻ" như "sao" Việt... là những tin tức nóng nhất trong ngày 23/05.

  • Thoi su 24h 2205 Mot trinh nu Viet gia toi thieu 3000 USD
    Thời sự 24h (22/05): Một trinh nữ Việt, giá tối thiểu 3000 USD

    Sốc giá tối thiểu 3.000 USD "mua" một trinh nữ Việt; Vụ “nam giới bán dâm”: Có cả giáo viên tham gia!; Hàn Quốc: Mối đe dọa từ Triều Tiên “chưa từng có”; Ăn sữa chua mít: Tự đầu độc mình?; Phẫn nộ clip nhóm thiếu niên trêu chọc cụ già; Sự khác biệt thú vị giữa trẻ con ngày ấy và bây giờ... là những tin nóng nhất trong ngày 21/05.

  • Thoi su 24h Bi tiem vac xin het dat chau be soc thuoc
    Thời sự 24h: Bị tiêm vắc xin "hết đát", cháu bé "sốc" thuốc

    Côn đồ truy sát nữ sinh: Dân lý giải chuyện đứng nhìn; Thu phí nhạc số: Không thành công cũng thành nhân; Vòi rồng khổng lồ san phẳng ngoại ô Mỹ, 51 người chết; Hàng ngàn người Hà Nội ăn nước phở bẩn mỗi sáng; Chuyện "đo đạc" thí sinh hoa hậu; Điệp khúc mất điện mùa nóng, chiêu tăng giá của EVN?... là những tin nóng nhất trong ngày 21/05

  • Thoi su 24h Hang chuc nguoi Viet Nam bi vi khuan an thit
    Thời sự 24h: Hàng chục người Việt Nam bị vi khuẩn 'ăn thịt'

    Thiết bị gián điệp tinh vi xuất hiện ở Hà Nội; M.U 5-5 West Brom: Sir Alex chia tay trong sự điên rồ; Hàn Quốc triển khai tên lửa sát biên giới Triều Tiên; Rùng rợn những cơn ghen mất tính người; Vi khuẩn “ăn thịt người” ở Việt Nam; “Sao” Việt dự LHP Cannes làm gì?; Một lít xăng đang cõng 4 loại thuế, 3 loại phí...là những tin nóng nhất trong ngày 20/05.

  • Thoi su 24h Phat tu sinh vien vi tuyen truyen chong nha nuoc
    Thời sự 24h: Phạt tù sinh viên vì tuyên truyền chống nhà nước

    Phạt tù cựu sinh viên vì "tuyên truyền chống nhà nước"; Nữ "dị nhân" HN nói chuyện được với cụ rùa Hồ Gươm?; Beckham chính thức nói lời từ giã bóng đá; Đài Loan phớt lờ Trung Quốc, khuyến cáo Philippines; 20 cặp đồng tính linh đình diễu hành và tổ chức đám cưới... là những tin nóng nhất trong ngày 17/05.