|
I have been programming with VB6 for many years and I am now in the process of “upgrading” to VB2008. I want to do all my work with code and not with data controls. (See my attached code so far). This is a useless sample just to get my head around a few things.
1. In the scores group on my form, I have 3 textboxes binded to my “BindingManagerBase” I can now move up and done both tables. I want to add “Delete”, “Add” and “Edit” functionality for the Scores table. Instead of manually looking up the StudentID, I want to replace the textbox with a Combobox. So I want to see the student’s “FirstName” from the students table in the combo box, and then link the StudentID from the Student Table to the StudentID in the TestScores table. (I have done it many times with VB6, but how do I do it in VB2008.
This is my tables and my code.
Table Name: Students
StudentID FirstName LastName
1 Amy Anderson
2 Bob Baker
3 Cindy Carter
4 Donald Dorph
Table Name: TestScores
StudentID TestNumber Score
1 1 98
1 2 94
2 1 74
3 2 65
' Data adapter for the Students table.
Dim m_daStudents As New OleDbDataAdapter( _
"SELECT Students.StudentID, Students.FirstName, Students.LastName " & _
"FROM Students", _
"PROVIDER=MSDataShape;" & _
"Data PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source='Student Scores.mdb';" & _
"Persist Security " & _
"Info=False")
' Data adapter for the TestScores table.
Dim m_daTestScores As New OleDbDataAdapter( _
"SELECT TestScores.StudentID, TestScores.TestNumber, TestScores.Score " & _
"FROM TestScores", _
"PROVIDER=MSDataShape;" & _
"Data PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source='Student Scores.mdb';" & _
"Persist Security " & _
"Info=False")
' The DataSet.
Dim m_DataSet As New DataSet("Student Scores")
' Declare a binding manager field
Public bm_Students As BindingManagerBase
Public bm_TestScores As BindingManagerBase
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Load the Students table.
m_daStudents.Fill(m_DataSet, "Students")
' Load the TestScores table.
m_daTestScores.Fill(m_DataSet, "TestScores")
' Get the DataTable objects.
Dim dt_students As DataTable = m_DataSet.Tables("Students")
Dim dt_testscores As DataTable = m_DataSet.Tables("TestScores")
' Get DataColumn objects we will need.
Dim col_students_studentid As DataColumn = _
dt_students.Columns("StudentId")
Dim col_testscores_studentid As DataColumn = _
dt_testscores.Columns("StudentId")
Dim col_testscores_testnumber As DataColumn = _
dt_testscores.Columns("TestNumber")
' Define the primary keys.
Dim students_keys() As DataColumn = _
{col_students_studentid}
dt_students.PrimaryKey = students_keys
Dim testscores_keys() As DataColumn = _
{col_testscores_studentid, col_testscores_testnumber}
dt_testscores.PrimaryKey = testscores_keys
' Make a foreign key constraint.
Dim fk_studentid As New ForeignKeyConstraint( _
col_students_studentid, col_testscores_studentid)
dt_testscores.Constraints.Add(fk_studentid)
' Bind to textboxes for Students table
txtStudentID.DataBindings.Add("text", m_DataSet, "Students.StudentID")
txtFirstName.DataBindings.Add("text", m_DataSet, "Students.FirstName")
txtLastName.DataBindings.Add("text", m_DataSet, "Students.LastName")
' Bind to textboxes for TestScore table
txtTestScores1.DataBindings.Add("text", m_DataSet, "TestScores.StudentID")
txtTestScores2.DataBindings.Add("text", m_DataSet, "TestScores.TestNumber")
txtTestScores3.DataBindings.Add("text", m_DataSet, "TestScores.Score")
' Bind to datagrid for TestScore table
DataGridView1.DataSource = m_DataSet
DataGridView1.DataMember = "TestScores"
bm_Students = BindingContext(m_DataSet, "Students")
bm_Students.Position = 0
bm_TestScores = BindingContext(m_DataSet, "TestScores")
bm_TestScores.Position = 0
End Sub
|
|
|
|
|
Have you tried binding the combobox to the datatable?
i.e.
ComboBox.DisplayMember = FieldToDisplay
ComboBox.ValueMember = ValueField
ComboBox.DataSource = DataTable
|
|
|
|
|
Thank you for your time. Yes, I have tried but it did not work. The syntax might be wrong. From VB6 I know that there are 5 properties to set:
1. You need to specify the table for the displayed value. (Table: Students)
2. You need to specify the field of the displayed value. (Field: FirstName)
3. You need to specify the field to transfer. (Field: StudentID)
4. You need to specify the table to be updated. (Table: Testscores)
5. You need to specify the field to be updated with the ComboBox. (Field: StudentID)
So maybe my downfall is thinking in VB6.
Back to your recommendation:
ComboBox.DisplayMember = FieldToDisplay What will this be in my case, based on the code
ComboBox.ValueMember = ValueField What will this be in my case, based on the code
ComboBox.DataSource = DataTable What will this be in my case, based on the code
Thank again for your time
|
|
|
|
|
Being a beginner myself I'm not that familiar with DataTables, but I think it should be something like:
ComboBox.DisplayMember = "Firstname"
ComboBox.ValueMember = "StudentId"
ComboBox.DataSource = dt_students
Hope this helps.
|
|
|
|
|
Thanks again for your time, I will try it tomorrow, I am not at my computer at the moment. Do you have an idea how to search for a record? Say for example I have a textbox and I enter a name in the textbox. I then want to go directly to record without looping through the table?
|
|
|
|
|
Again, being a beginner I may be wrong here but I think you can use the Select function to filter the rows in your datatable. Something like:
dt_Students.Select("FirstName = John")
|
|
|
|
|
Sorry, back to your last advice. This fills the combobox, but it is not linked to the Scores table. So if I hit the next button, I can step through the table, but the combobox will not reflect the correct student, it will stay with the first name.
|
|
|
|
|
Well if I understand you correctly, you need to handle the click event of the next button and then select the correct item in the ComboBox using: ComboBox.selectedValue = StudentID
|
|
|
|
|
What use to happen in VB6 is that you just add the following in the click event
bm_TestScores += 1
This causes you to move the row value up. Currently I use this to update the text values.
If I use the same "binding" for the combobox as for the textbox, the combobox update with the textbox when I hit the "next" button. Problem is it then shows the studentID and not the FirstName.
|
|
|
|
|
Hi,
How to invoke a .Net dll from vb script. While creating the object it is giving the Automation error. My OS is 2000 with .Net framework 1.1. The same script is working on another machine(configuration 2003R2 .Net framework 3.5).
Regards, Subramanyeswari
modified on Wednesday, February 4, 2009 1:06 AM
|
|
|
|
|
What do you mean by 'invoke' ? Are you trying to run it through COM ? What version of .NET was the dll written with ?
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
the .Net dll i think it is written in .Net 1.1. Invoke means when i am instantiating class I am getting the error.
Wscript.createobject("NetClass.cls").. here it is failing. it is giving "could not create object" error.
Regards
|
|
|
|
|
Your .NET component isn't registered as a COM server. You need to use the RegAsm tool to do that. You'll find that somewhere under C:\Windows\Microsoft.NET\Framework. Just search for RegAsm.exe and you'll find it.
|
|
|
|
|
hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please
<pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If Asc(e.KeyChar) = Keys.Enter Then
MsgBox("YES!")
e.Handled = True
End If
End Sub</pre>
J.Hardy
|
|
|
|
|
Hi,
you should use the KeyDown event, it offers KeyCode which directly compares to Keys.Enter without
needing the ASC function; then set SuppressKeyPress when you want to ignore the key.
BTW: you managed to use PRE tags the wrong way!
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
You need a global keyhook to do that, otherwise that code will only be triggered when your form as focus.
|
|
|
|
|
You can also set the form's KeyPreview property to True, then handle the form's KeyDown or KeyPress events to do the same thing.
|
|
|
|
|
I set KeyPreview = true in the form's properties dialog & then trap keydown
Private Sub frmInvoice_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyData = Keys.Enter Then
'bla, bla, bla
e.Handled = True
End If
End Sub
|
|
|
|
|
Set the KeyPreview property of the form to True , that would solve your problem
-Regards
Bharat Jain
bharat.jain.nagpur@gmail.com
|
|
|
|
|
hey guy thanks for all your help
J.Hardy
|
|
|
|
|
i got my control command to my external hardware. But the question now is i was unable to see the reply but i am sure there is a reply. Wat code should i insert to be able to see the reply?
|
|
|
|
|
why don't you try reading the data from the port?
What kind of question is this?
|
|
|
|
|
Please be clear as to what are you using, for now i am assuming that you you are using serial port component that comes with .net to revive any thing you must add an event handler for DataRecived event of serialPort component
You can the use serialPort1.ReadExisting() or serialPort.Read(), method to read from the serial port
Here is an link to a similar thread , related to receiving data from a serial port.
http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2897853[^]
Let me know if you need any more info
-Regards
Bharat Jain
bharat.jain.nagpur@gmail.com
|
|
|
|
|
Sorry for the unclear question. Ya wat i was trying to do was to retrieve data from my serial comm port. I am able to send to the comm port but not able to receive. I already seen the link u posted. I seen to be wat i am looking for but only ting is in C#. So i was wondering if u have the .net version?
|
|
|
|
|
this is the same code converted in vb
Private Sub SerialPort1_DataReceived(ByVal sender As
System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Handles SerialPort1.DataReceived
If SerialPort1.BytesToRead > 0 Then
Dim byteToRead As Integer
byteToRead = SerialPort1.BytesToRead
Dim byteArray(byteToRead) As Byte
Dim lineData As String
SerialPort1.Read(byteArray, 0, byteToRead)
lineData = ConvertToString(byteArray)
End If
End Sub
Private Function ConvertToString(ByVal byteArray As Byte()) As String
Dim tempStringToReturn As String = ""
For Each bt As Byte In byteArray
tempStringToReturn = tempStringToReturn + Convert.ToChar(bt)
Next
Return tempStringToReturn
End Function
hope this helps ...........
-Regards
Bharat Jain
bharat.jain.nagpur@gmail.com
|
|
|
|