question

RichGoldberg-2328 avatar image
0 Votes"
RichGoldberg-2328 asked RichGoldberg-2328 commented

Populating a Form Using a List

In VB.Net I am creating a table-driven form handler. In a public class I have defined:

 Public Class FormFld
     Public Property ScrField As Control ' The name of a control on the form
     Public Property DbField As String   ' Its corresponding field name in the database
     …                                   ' Other info about the field
 End Class

At the start of each form I code

 ReadOnly FormFlds As New List(Of FormFld)()

and in the form’s Load routine I code

 FormFlds.Add(New FormFld With {.ScrField = Control1Name, .DbField = "Field1Name", …})
 FormFlds.Add(New FormFld With {.ScrField = Control2Name, .DbField = "Field2Name", …})
 …

This technique has worked well to easily scan all the defined fields on a form and build a SQL Select statement based on the data in those fields.

Now I’m trying to reverse that process – A SQL Select statement has returned data to VB.Net and I’m trying to populate the form fields with that data.

 For Each formfld In FormFlds
     Me.(formfld.ScrField) = rs.(formfld.DbField) ' Copy the value from the db into the form field
 Next

where

 Dim cnnOLEDB As OleDbConnection
 Dim cmdOLEDB As New OleDbCommand
 Dim rs As OleDbDataReader
 cmdOLEDB.CommandText = "SELECT … FROM … WHERE …"
 cmdOLEDB.Connection = cnnOLEDB ' Submit the SQL command
 rs = cmdOLEDB.ExecuteReader

has already been performed.

Obviously the syntax “Me.(formfld.ScrField)” and “rs.(formfld.DbField)” don’t work, but I can’t think of a way to make it work. Can anyone suggest a viable solution?

(Note that my code is smarter than this simplified version. It checks for the various types of controls and knows how to load the data properly into each type of control.)














dotnet-visual-basic
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RichGoldberg-2328 ,
Could you please provide more information such as data stored in the database?
Do you want the form controls to display values from the database?

0 Votes 0 ·

@JiachenLiMFST-9349: Most fields are text, but there are booleans, integers, etc. I have other [unmentioned] information in the FormFld class (like whether the field is numeric, boolean, etc.) to allow me to properly move the database data in the proper format to the form controls. Also, since the actual control object is saved in the FormFld structure, my code can tell whether the destination is a text field or a checkbox, etc.

But let’s keep this simple. Assuming the database data for field ABC is text, and my form includes a control named TxtABC, I add its information to the list by

 FormFlds.Add(New FormFld With {.ScrField = TxtABC, .DbField = "ABC", …})

During the form load it will read the database, get the database data in field ABC, and store it in field TxtABC in the form. If I hardcoded this task it would look like

 cmdOLEDB.CommandText = "SELECT … FROM … WHERE …"
 cmdOLEDB.Connection = cnnOLEDB ' Submit the SQL command
 rs = cmdOLEDB.ExecuteReader
 Me.TxtABC = rs.Item("ABC") ' …The critical question…

The question is… I’m not hardcoding “Me.TxtABC” or “rs.Item(“ABC”), but pulling the names TxtABC and “ABC” from the FormFld. How can I code that final line using the data from the FormFld structure?










0 Votes 0 ·

My apologies for submitting my previous post as an answer instead of a comment.

0 Votes 0 ·

1 Answer

JiachenLiMFST-9349 avatar image
0 Votes"
JiachenLiMFST-9349 answered RichGoldberg-2328 commented

Hi @RichGoldberg-2328 ,
Try the code below.

 formfld.ScrField.Text = rs.item(formfld.DbField)

Best Regards.
Jiachen Li


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.





· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Tried it. On the right side of the equal sign, two issues are shown. "[formfld" shows error BC30034: "Bracketed identifier is missing closing ']'." And the right bracket shows error BC30037: "Character is not valid."

0 Votes 0 ·

Hi @RichGoldberg-2328 ,
Please check the updated code in the answer.

0 Votes 0 ·

@JiachenLiMFST-9349: My apologies for the delay in response. I had further issues accessing my database, but those issues are now corrected. Your suggested fix was perfect. Many thanks for your expertise and your time.

0 Votes 0 ·