Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > To bind your data to a data control, or to write your all the code yourself?


Reply
 
Thread Tools Display Modes
  #1  
Old 11-07-2002, 03:17 AM
Stewie
Guest
 
Posts: n/a
Default To bind your data to a data control, or to write your all the code yourself?


Hello
i have been reading up on ways to program data. and there appear to be two method
1) Bind the data
2) write the code your self to select which data

basicaly i have found lots of information on data binding and now have a good idea of it. but some books and web sites say that data binding is not the way to go because in the long term it is slower. and with writing code you only pull out what data you want. But other books say that data binding is too powerful to not use?
Anyway, i was wondering what your opinion is for what is the best method for the long run
and also if someone could paste a sample of code where they have written out the code themselves so that i could learn from it. because every book and web page that says writing it yourself is better just says "proffesional programmers write their code their selfs but we will look into databinding"
i cant actualy find any examples

another minor question

on a data control thing
instead of selecting a sql query to base my data on
could i not just NOT have a query command and just write the sql code as the record source?
it works, but im not sure if it is good practice or not

thanks for your help
Reply With Quote
  #2  
Old 11-07-2002, 03:59 AM
jkcontra
Guest
 
Posts: n/a
Default

Question 1) Coding is definately better than binding your data to a control. My experience with the ado data control is frustration after frustration.

I will write you a small app and mail it to you as soon as im Done, Give me your email address


Question 2) You can make queries in ACCESS and use that as your table. But I would rather use SQL queries
Reply With Quote
  #3  
Old 11-07-2002, 04:08 AM
Stewie
Guest
 
Posts: n/a
Default

okay briliant!
my email is jamjarman@hotmail.com

thanks again
Reply With Quote
  #4  
Old 11-07-2002, 04:24 AM
Stewie
Guest
 
Posts: n/a
Default

while on topic of coding it yourself

is there a way to globaly dim the database destination
so that i can refer to the dim all the time so if i was to change the destination of my database backend i wont have to go through every bit of code and change it?
i could have like database and database2 instead of //gggg///gggg sort of thing
Reply With Quote
  #5  
Old 11-07-2002, 06:33 AM
pup pup is offline
Newcomer
 
Join Date: Oct 2002
Posts: 14
Default

can you send me the code too? my mail is pup_viet@yahoo.com Thanks a lot.
Reply With Quote
  #6  
Old 11-07-2002, 07:07 AM
Stewie
Guest
 
Posts: n/a
Default

coutesy of jkcontra


'===================================================================== ==========='
' A simple ADODB connection. I Created an Access database just to get you
' started with the Basics. From there on you are welcome to ask me any
' questions.
' Refrences you need to set : Microsoft ActiveX Data Objects 2.5,2.6 or 2.7



Option Explicit

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String 'I will use this string to set the SQL Query
Dim strCN As String 'This string will be used for the connection string


Private Sub cmdAdd_Click()
'===================================================================== ==========='
' Adds a new record

txtCustomer(0).Text = ""
txtCustomer(1).Text = ""
txtCustomer(0).SetFocus
rs.AddNew
cmdExit.Caption = "Cancel"
End Sub



Private Sub cmdDelete_Click()
'===================================================================== ==========='
' Deletes a record
On Error Resume Next
Dim vPosition As Variant
vPosition = rs.AbsolutePosition - 1
rs.Delete
rs.Requery
rs.AbsolutePosition = vPosition
txtCustomer(0) = rs.Fields("Name")
txtCustomer(1) = rs.Fields("Surname")
End Sub

Private Sub cmdEdit_Click()
'===================================================================== ==========='
' I normally lock my textboxes. With my Edit button I unlock my textboxes
' Im just using Edit button to select the first Textbox.
txtCustomer(0).SetFocus
cmdExit.Caption = "Cancel"
End Sub

Private Sub cmdExit_Click()
On Error Resume Next
If cmdExit.Caption = "Exit" Then
rs.Close
cn.Close
End
Else
rs.CancelUpdate
rs.MoveFirst
txtCustomer(0).Text = rs.Fields("Name")
txtCustomer(1).Text = rs.Fields("Surname")
cmdExit.Caption = "Exit"
End If
End Sub

Private Sub cmdFirst_Click()
'===================================================================== ==========='
' Moves to the first record
On Error Resume Next
rs.MoveFirst
txtCustomer(0).Text = rs.Fields("Name")
txtCustomer(1).Text = rs.Fields("Surname")
If rs.AbsolutePosition < 0 Then rs.MoveFirst
End Sub

Private Sub cmdLast_Click()
'===================================================================== ==========='
' Moves to the last record
On Error Resume Next
rs.MoveLast
txtCustomer(0).Text = rs.Fields("Name")
txtCustomer(1).Text = rs.Fields("Surname")
If rs.AbsolutePosition < 0 Then rs.MoveLast
End Sub

Private Sub cmdNext_Click()
'===================================================================== ==========='
' Moves to the next record
On Error Resume Next
rs.MoveNext
txtCustomer(0).Text = rs.Fields("Name")
txtCustomer(1).Text = rs.Fields("Surname")
If rs.AbsolutePosition < 0 Then rs.MoveLast
End Sub

Private Sub cmdPrevious_Click()
'===================================================================== ==========='
' Moves to the previous record
On Error Resume Next
rs.MovePrevious
txtCustomer(0).Text = rs.Fields("Name")
txtCustomer(1).Text = rs.Fields("Surname")
If rs.AbsolutePosition < 0 Then rs.MoveFirst
End Sub

Private Sub cmdSave_Click()
'===================================================================== ==========='
' Save a new or edited record

rs.Fields("Name") = txtCustomer(0).Text
rs.Fields("Surname") = txtCustomer(1).Text
rs.UpdateBatch adAffectCurrent
If rs.BOF Then
txtCustomer(0).Text = ""
txtCustomer(1).Text = ""
Else
txtCustomer(0).Text = rs.Fields("Name")
txtCustomer(1).Text = rs.Fields("Surname")
End If
cmdExit.Caption = "Exit"
End Sub

Private Sub Form_Load()
'===================================================================== ==========='
' Starts the connection and set the recordset
Dim iCount As Integer 'Icount will select the textbox which to add text
Set cn = New ADODB.Connection
strCN = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\Simple.mdb;" & _
"Mode=ReadWrite|Share Deny None;" & _
"Persist Security Info=False"
With cn
.ConnectionTimeout = 30
.ConnectionString = strCN
.Open
End With
strSQL = "Select * from Customer Order by Surname, Name"
Set rs = New ADODB.Recordset
rs.Open strSQL, cn, adOpenKeyset, adLockOptimistic

If Not rs.BOF Then
For iCount = 0 To 1
txtCustomer(iCount).Text = rs.Fields(iCount + 1)
Next iCount
End If

Me.Left = (Screen.Width - Me.Width) / 2
Me.Top = (Screen.Height - Me.Height) / 2
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
cmdExit_Click
End Sub
Reply With Quote
  #7  
Old 11-07-2002, 07:09 AM
Stewie
Guest
 
Posts: n/a
Default

I am having a problem here. everything works perfect. except for the line in bold. it gives me a message "invalid use of a null vale"


rs.MoveFirst
txtEmpID.Text = rs.Fields("EmpID")
txtEmployeeID.Text = rs.Fields("EmployeeID")
txtFirstName.Text = rs.Fields("Firstname")
txtLastName.Text = rs.Fields("Lastname")
txtAddress.Text = rs.Fields("Address")
txtAddress2.Text = rs.Fields("Address2")
txtPostalCode.Text = rs.Fields("postalcode")
Reply With Quote
  #8  
Old 11-07-2002, 07:18 AM
GMan_NC's Avatar
GMan_NC GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

That's because Address2 is Null. You will have to check for this. There are a few ways to do this, here's one.

Code:
if Not IsNull(rs.Fields("Address2)) Then txtAddress2.Text = rs.Fields("Address2")

You will need to do this for any field that may contain a Null value
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #9  
Old 11-07-2002, 07:20 AM
Stewie
Guest
 
Posts: n/a
Default

Gman you are THE MAN
Reply With Quote
  #10  
Old 11-07-2002, 07:23 AM
Stewie
Guest
 
Posts: n/a
Default

hey gman, it says to me everytime i try to put in the code
compile error: expected seperator or )

i tried playing around with it but no luck
no luck at all my friend
Reply With Quote
  #11  
Old 11-07-2002, 07:28 AM
GMan_NC's Avatar
GMan_NC GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

i failed to add a closed "

Code:
If Not IsNull(rs.Fields("Address2")) Then txtAddress2.Text = rs.Fields("Address2")
__________________
A morning without coffee is like something without something else.
Reply With Quote
  #12  
Old 11-07-2002, 07:36 AM
Stewie
Guest
 
Posts: n/a
Default

works like a charm. i didnt see that bit

**** i actualy have a section of my prject working now lol
Reply With Quote
  #13  
Old 11-07-2002, 07:38 AM
Stewie
Guest
 
Posts: n/a
Default

hey jkcontra has tipped me off with this handy little thing

On Error Goto ErrMsg
Then before end sub put his is

ErrMsg:
If err.number = 94 then
Resume next
End if
Reply With Quote
  #14  
Old 11-07-2002, 10:36 AM
greg8872
Guest
 
Posts: n/a
Default

I was just wondering, is doing

rs.Fields("Address2")

the exact same as doing

rs![Address2]

I first learned to do the second method, and wished at times I could use a vairable for the field name...

-Greg
Reply With Quote
  #15  
Old 11-07-2002, 10:38 AM
GMan_NC's Avatar
GMan_NC GMan_NC is offline
Proud Daddy

Forum Leader
* Expert *
 
Join Date: Sep 2002
Location: North Carolina, USA
Posts: 1,962
Default

if you want to use a variable for the field name, then use the first method

rs.Fields(VarName)
__________________
A morning without coffee is like something without something else.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->