Log in

View Full Version : Starting out with Databases -need help desperately!


rainerf
01-12-2003, 12:16 AM
I am new to eVB and VB (but do have some programming experience with VFP). That said, I very much need a quick start in deleoping a database project. It would be great if it could directly connect to VFP database, but ActiveSynch is giving me problems in the conversion (stating that fileds have an incorrect decimal field, but, I've used several samples of dbc's, some even without numeric fields and using a char field for the primary key).
If someone could help me initially:

Databse with, say, 3 fields: 'Name', 'Sequence' (primary key), 'Value'

Form needs: Pulldown ComboBox (associated to 'Name')
TextBox (associated to 'Value')

When a name is selected from the ComboBox, form needs to refresh to show the associated 'Value' field.

That's it! If someone can help me with that, I can probably get a good idea on how data connections are associated. :?:

I am in a time bind, so, any help would be greatly appreciated!

Rainer

Peter Foot
01-12-2003, 03:19 PM
A good place to start with eVB and Database development is deVBuzz.com, in particular this article is a good starter:-

http://www.devbuzz.com/content/zinc_intro_ADOCE_databases_pg1.asp

Peter

rainerf
01-12-2003, 06:14 PM
Peter,
thank you for the reply, and, I have been using DeVBuzz for the past two weeks and did get a lot of info out of it and did make some headway in understanding a little VB. Unfortunately, even after buying a eVB book by 'Tacke', I fail to understand one primitive and basic prinicpal: How to display and change a tables field.
I used deVBuzz's sample (attached below) to make a form and display a ComboBox, and this actually works. But I am unable to add a field to the form and link it to a second field in the tble's recordset (as I mentioned above).

I am really trying to do my research, but can't find the answer. And, looking into VB (rather than eVB), fields on the form have an associated 'DAta' property, which does not exist in eVB. So, again, I really am in desperate need of help!
The sample code is below, and I need to know how to add another field and link that. :(
+++++++++++++++++++++++++++++++
Option Explicit

Public conndb As ADOCE.Connection

Sub LoadCtrl(paramCtrl As ComboBox, paramSQL As String)
Dim rsCtrl As ADOCE.Recordset
Dim val1 As Integer
Dim val2 As String
Set rsCtrl = CreateObject("ADOCE.RecordSet.3.0")
rsCtrl.Open paramSQL, conndb, adOpenForwardOnly, adLockReadOnly
If rsCtrl.RecordCount > 0 Then
Do While Not rsCtrl.EOF
If IsNumeric(Trim(rsCtrl.Fields(0))) Then _
val1 = CInt(rsCtrl.Fields(0))
val2 = rsCtrl.Fields(1)
paramCtrl.AddItem val2
paramCtrl.ItemData(paramCtrl.NewIndex) = val1
rsCtrl.MoveNext
Loop
End If
rsCtrl.Close
Set rsCtrl = Nothing
End Sub


Private Sub cmbName_Change()

End Sub

Private Sub Form_Load()


'Connect to the database
Set conndb = CreateObject("ADOCE.Connection.3.0")
conndb.ConnectionString = "data source = \My Documents\VFPpt.cdb"
conndb.Open

Call LoadCtrl(Form1.cmbName, "select sequence,name FROM pocket ORDER BY name")

conndb.Close
Set conndb = Nothing

End Sub
+++++++++++++++++++++++++++++

Peter Foot
01-12-2003, 07:52 PM
Ok, If I understand correctly you want to display a record based on what the user picks from this combobox you have sucessfully populated.

You should add code to the cmbName_Change() event - this will be fired when the user changes the currently selected item in the combobox

Private Sub cmbName_Change()

'step 1 determine the value of the selected item
Dim SelectedItem As String
SelectedItem = cmbName.SelText


'step 2 open a recordset with this value as criteria
Dim rsChosenItem As ADOCE.Recordset
Set rsChosenItem = CreateObject("ADOCE.RecordSet.3.0")
'build a select statemnt to pick the record matching your choice
Dim sql As String
sql = "SELECT * FROM pocket Where name = '"
sql = sql + SelectedItem
sql = sql + "'"

rsChosenItem.Open sql, conndb, adOpenForwardOnly, adLockReadOnly

'step 3 read the resulting data into your output controls eg text box
'only read the value if there is a result
If rsChosenItem.RecordCount <> 0
'you will need a textbox on your form, use its name here
'to assign the value
txtValue.Text = rsChosenItem.Fields("Value").Value
End If

'close and dispose of recordset
rsChosenItem.Close
Set rsChosenItem = Nothing

End Sub

Please note my eVB is a bit rusty and this is untested, I've been working with .NET for the past months, however hopefully this should get you on the right track.

rainerf
01-12-2003, 10:36 PM
Peter,
thanks a million for the code! I can make sence out of it and it is exactly what I need to get me in the right direction!

Thank you very much, I will try to implement it later tonight!


Glad there is help like yours on these forums,

Rainer