CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2010
    Posts
    2

    Post How do I find the first record on my form from a combobox in Visual Basic 2010

    This is well documented and easy to accomplish in Access, but how does one accomplish this in Visual Basic 2010 on a form designed in Visual Studio that is bound to a SQL Server database?

    The idea is to create a combobox that holds 2 columns - a primary key value and a unique value such as a [Company Name] or a [Student Name]. The user would then find the appropriate record within the combobox and the code behind the combobox control would then perform a .findfirst method that would NOT filter out all other records, but would rather use selectedvalue primary key column value to move to the selected record.

    I am currently getting into VS 2010 & revisiting VB having been programing in ACCESS & VBA for years. In the Access/VBA environment it is very common to have to find a given record on a given form using this method, but it would appear to be poorly documented if in fact not commonly used in the VB.NET/VS community.

  2. #2
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: How do I find the first record on my form from a combobox in Visual Basic 2010

    I don't know the objects you are using but assuming you have a DataGridView and a BindingSource (BS) as the DGV DataSource you can use:

    BS.Position = BS.Find(KeyName, Value)

  3. #3
    Join Date
    Jul 2010
    Posts
    2

    Re: How do I find the first record on my form from a combobox in Visual Basic 2010

    The best way to explain the problem is to demonstrate what I would normally do, so...

    THE ACCESS Solution
    ================
    I have an .MDB file that I have created and have uploaded it here.

    This file will open up a students form as the initial form.

    On the form, in the header you will find a ComboBox and a 2-Button option box.

    If you choose a student from the ComboBox you will quickly locate the record associated to the student.

    Also, if you choose to search by First Name instead of Surname, then you select a button below the search combobox that changes the rowsource of the combobox.

    This is a very typical form that has to be designed as is efficient. As such it is an absolute requirement in all Visual Studio applications that I will need to develop.

    The code behind the ComboBox can be viewed within access, but is pasted here for convenience:

    Private Sub FindStudent_AfterUpdate()
    'There are 2 possible methods that can be employed to locate a given record here by using VBA.
    'Option 1: Using the FindRecord Option
    '=======================================================================
    ' DoCmd.GoToControl "studentid"
    ' DoCmd.FindRecord Me.Combo21, acStart, , acDown, , acCurrent, True
    'Option 2: Using the SearchRecord option (preferred)
    DoCmd.SearchForRecord acDataForm, "frm_Students", acFirst, "studentID=" & CStr(Nz(Me.FindStudent.Value, ""))

    End Sub

    THEN...
    I have done the same solution, but this time in VS 2010, with SQL Server as the DB.
    I have uploaded the DB as a SQL Server .BAK backup file and the script to create the DB is contained in the StudentCourses.ZIP file - along with the entire VS solution.

    I think that this will become a nice tutorial exercise, so I thought that it would be good if I was to upload both solutions here to be reviewed.

    As you will see, the VS solution pops up a msgbox with the selectedvalue of the combobox.

    I would like to use the BindingSource.Find method, but am uncertain of how to proceed.

    Can someone please look at this solution and advise?

    Sean
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured