Results 1 to 6 of 6

Thread: [RESOLVED] Selecting RECORDS from an MSFlexGrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Resolved [RESOLVED] Selecting RECORDS from an MSFlexGrid

    Hey all,
    Im using si_the_geek's method for filling an MSFlexGrid from a DB as stated here: Fill a MSFlex from DB

    now im trying to populate Textboxes with values from the record that is selected from the Grid. I originally had planned on using the .TextMatrix props to get it done, but i need data from some of the fileds that i dont display on the Grid. So im trying to find a way to use something like i did with a ListView where i use a .Tag each time data is inserted to the Grid and then calling on it, but i cant find good documentation on how i would accomplish this with a FlexGrid.

    Anyone have any suggestions ?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: Selecting RECORDS from an MSFlexGrid

    well i finally figured it out, but i had to use a datacontrol again, and seems like when ever i try and get away from them i have to use them again.

    The part that i hate is that the "PhoneNumber" field is the primary key but i dont want to display that filed on the FlexGrid, so i just set the column width = 0 for that field so that it would still be there to call, but still not visible. then i just used the datacontrol to fill the txt boxes:

    VB Code:
    1. Private Sub MSFlexGrid1_Click()
    2. Dim var1 As String
    3.     var1 = MSFlexGrid1.TextMatrix(MSFlexGrid1.RowSel, 5)
    4.     Set cn = New ADODB.Connection
    5.     cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    6.       "Data Source= F:\DB1.mdb"
    7.     cn.Open
    8. strSQL = "SELECT Userid, CreateDateTime, Notes FROM CustomerNotes WHERE PhoneNumber = '" & var1 & "'"
    9.     Set rs = New ADODB.Recordset
    10.     rs.Open strSQL, cn, adOpenKeyset
    11.  
    12. Call FillFromRecordset_FlexGrid(Form1.MSFlexGrid1, rs, True)
    13. Call FlexGrid_colors1 'Fill color sub for Form1
    14.  
    15. strSQL = "SELECT * FROM Customers WHERE PhoneNumber = '" & var1 & "'"
    16. datFind.RecordSource = strSQL
    17. datFind.Refresh
    18.  
    19. Form1.txtClientName.Text = datFind.Recordset("FirstName") & " " & datFind.Recordset("LastName")
    20. Form1.txtAddress.Text = datFind.Recordset("Address1")
    21. Form1.txtCity.Text = datFind.Recordset("City")
    22. Form1.txtState.Text = datFind.Recordset("State")
    23. Form1.txtZip.Text = datFind.Recordset("zipcode")
    24. Form1.txtHomePhone.Text = datFind.Recordset("PhoneNumber")
    25. Form1.txtEmail.Text = datFind.Recordset("Email")
    26.    
    27. Form4.Hide
    28. Form1.Show
    29. End Sub

    So im REALLY hoping there is an easier way to do this ... if anyone has some suggestions !
    Last edited by Red_Dragon_X; Sep 11th, 2006 at 01:48 AM. Reason: cover info

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Selecting RECORDS from an MSFlexGrid

    If the unique field is numeric, you can use the RowData property (of each row) to store it, but as the field is text then the solution you have is one of the better methods.

    I have amended your sub below, with a few improvements.

    There is no need to ever use the data control, you can simply use a recordset variable (either another one, or re-use the same one). Once each type of recordset has been loaded, datFind.Recordset("FirstName") is the same as rs("FirstName") , but I have been more specific in the code below (it's slightly faster, and is less error prone too).

    When you have finished with a recordset (or any other object variable, such as cn) you need to close it if appropriate, then set it to Nothing. This frees memory, and stops any conflicts with the objects (in the case of recordsets and connections, you might stop another user from working with the same database, even when your program has closed).

    I have not put in code to close (or declare) the connection, as you can safely keep it open the entire time your program is open - but you need to close it properly when your program closes. I would normally open the connection at the start of the program, and close it at the end, but your usage determines if you should do the same (or declare & open & close it in this sub).

    VB Code:
    1. Private Sub MSFlexGrid1_Click()
    2. Dim var1 As String
    3.     var1 = MSFlexGrid1.TextMatrix(MSFlexGrid1.RowSel, 5)
    4.     Set cn = New ADODB.Connection
    5.     cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    6.       "Data Source= F:\DB1.mdb"
    7.     cn.Open
    8.  
    9. Dim rs As ADODB.Recordset
    10.     strSQL = "SELECT Userid, CreateDateTime, Notes FROM CustomerNotes WHERE PhoneNumber = '" & var1 & "'"
    11.     Set rs = New ADODB.Recordset
    12. 'use the most restrictive options that allow what you need - in this case, the most restrictive there is!
    13.     rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly
    14.  
    15.     Call FillFromRecordset_FlexGrid(Form1.MSFlexGrid1, rs, True)
    16.     Call FlexGrid_colors1 'Fill color sub for Form1
    17.  
    18.     rs.Close    'close object, so we can re-use it
    19.  
    20.     strSQL = "SELECT * FROM Customers WHERE PhoneNumber = '" & var1 & "'"
    21.     rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly
    22.  
    23.     Form1.txtClientName.Text = rs.Fields("FirstName").Value & " " & rs.Fields("LastName").Value
    24.     Form1.txtAddress.Text = rs.Fields("Address1").Value
    25.     Form1.txtCity.Text = rs.Fields("City").Value
    26.     Form1.txtState.Text = rs.Fields("State").Value
    27.     Form1.txtZip.Text = rs.Fields("zipcode").Value
    28.     Form1.txtHomePhone.Text = rs.Fields("PhoneNumber").Value
    29.     Form1.txtEmail.Text = rs.Fields("Email").Value
    30.    
    31.     rs.Close    'close object, and clear memory
    32.     Set rs = Nothing
    33.    
    34.     Form4.Hide
    35.     Form1.Show
    36.  
    37. End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: Selecting RECORDS from an MSFlexGrid

    YAY !!! i got to delete another Data Control on my forms !! LOL. THANKS !

    btw, do you know much about emails via:
    VB Code:
    1. ShellExecute 0, vbNullString, "mailto:" & txtEmail & "?subject=""&body="", vbNullString, vbNullString, vbNormalFocus

    im trying to get it to populate a draft(from outlook, but it could be in any format) that i have into the body of the email, that way i can have a new email pop up with the subject and body already filled out.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Selecting RECORDS from an MSFlexGrid

    Well that's a lucky coincidence... I can't remember what reminded me of it, but I found this this other day:
    http://www.vbforums.com/showthread.php?t=326272
    ..post 6 contains a sub which can create it with a subject/body/etc.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: [RESOLVED] Selecting RECORDS from an MSFlexGrid

    Yes thats perfect !! thank you soo much !

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