Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > Vb6:button for saving check box into flexgrid


Reply
 
Thread Tools Display Modes
  #1  
Old 10-24-2010, 08:43 AM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default Vb6:button for saving check box into flexgrid


For I = 0 To MSFlexGrid1.Row - 1
sql = "INSERT INTO Lab "
sql = sql & "(Class,equipment,no,select)"
sql = sql & " VALUES "
sql = sql & "('"
sql = sql & Combo1.Text & " ','"
sql = sql & MSFlexGrid1.TextMatrix(I + 1, 1) & " ','"
sql = sql & MSFlexGrid1.TextMatrix(I + 1, 2) & " ','"
sql = sql & MSFlexGrid1.TextMatrix(I+1, 3) & " ')"
Next I
Error : Data type mismatch in criteria expression.SELECT is a check box. In database, I set the data type of ' SELECT' is YES/NO. .TextMatrix is not matching with the data type of ' SELECT' is YES/NO. Should I replace .TextMatrix with other command?
Reply With Quote
  #2  
Old 10-24-2010, 01:00 PM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,929
Default

I assume you mean the type of field "select" is BOOLEAN.
You now try to put a literal text value in the in db.

Which text values do you have in column 3 of the MSFlexGrid?
Reply With Quote
  #3  
Old 10-24-2010, 08:06 PM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default Vb6:button for saving check box into flexgrid

What is literal text, give me the example.

Last edited by ndsaid; 10-24-2010 at 08:15 PM.
Reply With Quote
  #4  
Old 10-25-2010, 12:50 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,929
Default

What do you see/show in the FlexGrid in column 3?
Reply With Quote
  #5  
Old 10-25-2010, 02:25 AM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default Vb6:button for saving check box into flexgrid

Column 3 is a check box
Reply With Quote
  #6  
Old 10-25-2010, 02:56 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,929
Default

How did you put the checkboxes in the MSFlexGrid?
The MSFlexGrid does not support checkboxes.
Reply With Quote
  #7  
Old 10-25-2010, 03:58 AM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default Vb6:button for saving check box into flexgrid

I'm new in visual basic 6, Which one component control support check box?
It is Data grid or MShFlexgrid?
Reply With Quote
  #8  
Old 10-25-2010, 04:27 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,929
Default

Please back to your original question.
You want to update your database with a query.

Is one of the fields (column 3) in the table defined as a Boolean?
You want to update this field with the value in MSFlexGrid.TextMatrix(Row, 3)
How do you fill the MSFlexGrid with data?

Which values do you see on the screen in the MSFlexGrid in column 3?
Reply With Quote
  #9  
Old 10-25-2010, 09:41 PM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default Vb6:button for saving check box into flexgrid

I don't defined field as boolean
I fill MSFlexGrid with data using command INSERT INTO.
Reply With Quote
  #10  
Old 10-26-2010, 01:02 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,929
Default

There are a lot of language problems here...

Quote:
I fill MSFlexGrid with data using command INSERT INTO.
You can't not fill the MSFlexGrid with an INSERT query.

Quote:
In database, I set the data type of 'SELECT' is YES/NO.
..
I don't defined field as boolean
You say have a field defined as "YES/NO".
If you are using MSAccess then this field will be of type boolean.

Quote:
Code:
For I = 0 To MSFlexGrid1.Row - 1
  sql = "INSERT INTO Lab "
  sql = sql & "(Class,equipment,no,select)"
  sql = sql & " VALUES "
  sql = sql & "('"
  sql = sql & Combo1.Text & " ','"
  sql = sql & MSFlexGrid1.TextMatrix(I + 1, 1) & " ','"
  sql = sql & MSFlexGrid1.TextMatrix(I + 1, 2) & " ','"
  sql = sql & MSFlexGrid1.TextMatrix(I+1, 3) & " ')"
Next I
You can use the data in the MSFlexGrid to create an INSERT query to update a database table.
But if field "select" is of type boolean, you have to use something like this:
Quote:
Code:
Dim bValue As Boolean
For I = 0 To MSFlexGrid1.Row - 1
  sql = "INSERT INTO Lab "
  sql = sql & "(Class,equipment,no,select)"
  sql = sql & " VALUES "
  sql = sql & "('"
  sql = sql & Combo1.Text & " ','"
  sql = sql & MSFlexGrid1.TextMatrix(I + 1, 1) & " ','"
  sql = sql & MSFlexGrid1.TextMatrix(I + 1, 2) & " ','"
  bValue = (LCase(MSFlexGrid1.TextMatrix(I+1, 3)) = "yes")
  sql = sql & CStr(bValue) & " ')"
Next I
What do you see in your current MSFlexGrid?
Please show some code instead of posting one liners.
Reply With Quote
  #11  
Old 10-26-2010, 02:28 AM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default

I have paste this code. I got error: Syntax error in INSERT INTO statement
Reply With Quote
  #12  
Old 10-26-2010, 02:39 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,929
Default

Show the actual generated SQL statement!

Also post MORE of your actual code!
Reply With Quote
  #13  
Old 10-26-2010, 02:46 AM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default Vb6:button for saving check box into flexgrid

Private Sub cmdSave_Click()
Call Module1.conn
Call Module1.Recordset

sql = "INSERT INTO Lab "
sql = sql & "(Class,equipment,no,select)"
sql = sql & " VALUES "
sql = sql & "('"
sql = sql & Combo1.Text & " ','"
sql = sql & MSFlexGrid1.TextMatrix(I + 1, 1) & " ','"
sql = sql & MSFlexGrid1.TextMatrix(I + 1, 2) & " ','"
sql = sql & MSFlexGrid1.Text Matrix(I + 1, 3) & "')"
Next I

If rs.State = adStateOpen Then rs.Close

rs.Open sql, cn, adOpenDynamic, adLockOptimistic
Set rs = Nothing

End Sub


This my code.
Reply With Quote
  #14  
Old 10-26-2010, 05:42 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,929
Default

What is the content of the MSFlexGrid?
Reply With Quote
  #15  
Old 10-26-2010, 06:03 AM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default Vb6:button for saving check box into flexgrid

Class= Name of class like M1BK1,M1BK2
Equipment= Chair,desk,blackboard
No=the number of equipment like 2,20,1
Select= a checkbox
Reply With Quote
  #16  
Old 10-26-2010, 06:21 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,929
Default

If I ask for the content of the MSFlexGrid you post the content of the table.
If I ask for the code to fill the grid you post the code of updating the table.

Can you please post your complete program including the database file?
Because you really lost me.
Reply With Quote
  #17  
Old 10-26-2010, 06:55 PM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default Vb6:button for saving check box into flexgrid

For latest error: Syntax error in Insert into statement

For connection to database, I dont use App.Path because I dont know to use it. I use for connection, for example F:\Project1\try1.mdb
Attached Files
File Type: zip Project1.zip (15.7 KB, 14 views)
Reply With Quote
  #18  
Old 10-27-2010, 02:49 AM
gibra's Avatar
gibra gibra is offline
Junior Contributor
 
Join Date: Apr 2008
Location: Italy
Posts: 377
Default

Quote:
Originally Posted by ndsaid View Post
For I = 0 To MSFlexGrid1.Row - 1
sql = "INSERT INTO Lab "
sql = sql & "(Class,equipment,no,select)"
sql = sql & " VALUES "
sql = sql & "('"
sql = sql & Combo1.Text & " ','"
sql = sql & MSFlexGrid1.TextMatrix(I + 1, 1) & " ','"
sql = sql & MSFlexGrid1.TextMatrix(I + 1, 2) & " ','"
sql = sql & MSFlexGrid1.TextMatrix(I+1, 3) & " ')"
Next I
Error : Data type mismatch in criteria expression.SELECT is a check box. In database, I set the data type of ' SELECT' is YES/NO. .TextMatrix is not matching with the data type of ' SELECT' is YES/NO. Should I replace .TextMatrix with other command?

Arrrrgh....
You DON'T say that use a different font in (CellFontName = "Wingdings") to show the checkbox!
This is an important information to understand your problem!!!!!!!!!

In fact, your sql string statement return this:

Code:
INSERT INTO Lab2(Class,equipment,No,[Select]) VALUES ('M3bK1 ','Chair ','20 ','þ')
where 'þ' value is a String type, not a Boolean type (Yes/No).

There are a lot of problems:

1. Of course, you CAN'T update a Boolean field (YES/NO) with a String value.
Therefore you must translate String value type to Boolean value type.


2. Select and No are both reserved words of SQL language, so you have to replace them: i.e. you can use SelectYN and Nr, instead.
Otherwise the name fields must be indicates each time with [], i.e.:
Code:
INSERT INTO Lab2 (Class,equipment,[No],[Select])
My suggestion is: name are wrong, replace them.


3. Pay attention: in your sql statement you add a extra-space in each TextMatrix reading.


4. Actually, you open the connectionstring of conn using a Public Const try1... , this is wrong!

In Module1.bas use
Code:
Public try1 As String
While in Form_Load() use App.Path to get the database path
Code:
try1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\try1.mdb"

5. In Module1.bas you declare TotRecord as Integer, if you use the variable to store records count then you have to declare as Long, not as Integer
Code:
Public TotalRec As Long

6. Code for Save
Code:
Private Sub cmdSave_Click()
    Rem ------------------------------------------------------------
    'Call Module1.conn      ' connection already open in Form_Load()!!!
    'Call Module1.Recordset ' don't need a new recordset to save data
    Rem ------------------------------------------------------------
    
    Dim lngRecordsAffected As Long
    Dim sSelect As String

    For i = 0 To MSFlexGrid1.Row - 1
        sql = "INSERT INTO Lab2 "
        sql = sql & "(Class,Equipment,[No],[Select])"
        sql = sql & " VALUES "
        sql = sql & "('"
        sql = sql & Combo1.Text & "','"
        sql = sql & MSFlexGrid1.TextMatrix(i + 1, 1) & "','"
        sql = sql & MSFlexGrid1.TextMatrix(i + 1, 2) & "',"
        'sql = sql & MSFlexGrid1.TextMatrix(i + 1, 2) & " ','"  '<- wrong
        'sql = sql & MSFlexGrid1.TextMatrix(i + 1, 3) & "')"    '<- wrong
        
        Rem You need to translate value for Yes/No field
        sSelect = IIf(MSFlexGrid1.TextMatrix(i + 1, 3) = "þ", "-1", "0")
        
        sql = sql & sSelect & ")"
        
        Debug.Print sql     '<- IMPORTANT to see what sql string contains!
        'Rem I can use 'Stop' temporally to pause execution and check sql statement
        'rem in Immediate window. Remove or comment before compile
        Stop
        
        Rem Save each row
        cn.Execute sql, lngRecordsAffected

        Debug.Print "row saved: " & lngRecordsAffected
    Next i
        
    ' save data
    
    Rem ------------------------------------------------------------
    Rem Wrong! Don't need a recordset
    Rem ------------------------------------------------------------
    'If rs.State = adStateOpen Then rs.Close
    'rs.Open sql, cn, adOpenDynamic, adLockOptimistic
    'Set rs = Nothing

End Sub
Regards

EDIT
Please read the routine Sub conn() on Module1.bas
Attached Files
File Type: zip ndsaid-msflexgrid-InsertInto.zip (13.9 KB, 29 views)
Reply With Quote
  #19  
Old 10-27-2010, 03:01 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,929
Default

I had to do lot of cleaning up.
You really shouldn't use that many PUBLIC variables.

The SQL INSERT error you got is because of using reserved SQL syntax words as field names in your tables.

Code:
Option Explicit

Private Const FLEXYES As Integer = 254
Private Const FLEXNO As Integer = 168

'Dim TR As Long, indx As Long

Private Sub cmdSave_Click()
  Dim sSQL As String
  Dim sSelect As String
  Dim lRow As Long
  Dim adoConn As ADODB.Connection
  
  Set adoConn = OpenConnection
  
  ' Because you used some reserved words as column names in your
  ' MS Access tables you have to use [] around the field names
  
  ' Using SQL INSERT will add a new record EVERY time.
  ' Also if the specific "class,equiment,no" already exists!
  With MSFlexGrid1
    For lRow = .FixedRows To .Rows - 1
      sSQL = "INSERT INTO Lab2 ([Class], [Equipment], [No], [Select])"
      sSQL = sSQL & " VALUES "
      sSQL = sSQL & "('"
      ' Be carefull with spaces with between ' '
      sSQL = sSQL & Combo1.Text & "', '"
      sSQL = sSQL & .TextMatrix(lRow, 1) & "', '"
      sSQL = sSQL & .TextMatrix(lRow, 2) & "', "
      If Asc(.TextMatrix(lRow, 3)) = FLEXYES Then sSelect = "1" Else sSelect = "0"
      sSQL = sSQL & sSelect & ")"
      ' No need to use a recordset if you want to use an UPDATE or INSERT query
      adoConn.Execute sSQL
    Next lRow
  End With
  
  CloseConnection adoConn
End Sub

Private Sub Combo1_Click()
  Call MyFlexiData
End Sub


Private Sub Form_Load()
  Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2 'Center the form
  
  With MSFlexGrid1
    .FormatString = "<ID|<Equipment|<No|<Select"
    .ColWidth(0) = 0.3 * 1440
    .ColWidth(1) = 1 * 1440
    .ColWidth(2) = 1.43 * 1440
    .ColWidth(3) = 600
    .Row = 0                                 '* -> these three steps decide the position of text in a cell
    .CellAlignment = flexAlignCenterCenter   '*
    .Rows = 1
  End With
  
  Call MyCombo
  Call MyFlexiData
  
End Sub

Private Sub MyCombo()
  'for Data List of Block Combo
  Combo1.Clear
  Combo1.AddItem "M3bK1"
  Combo1.AddItem "M2bK1"
  Combo1.AddItem "M1bK1"
End Sub

Private Sub MyFlexiData()
  Dim sSQL As String
  Dim adoRS As ADODB.Recordset
  Dim adoConn As ADODB.Connection
  
  If VBA.Trim(Combo1.Text) = "" Or VBA.Trim(Combo1.Text) = "M2bK1" Then
     sSQL = "SELECT * FROM Lab1 WHERE Class='" & VBA.Trim(Combo1.Text) & "' ORDER by Equipment"
  Else
    If VBA.Trim(Combo1.Text) = "" Or VBA.Trim(Combo1.Text) = "M3bK1" Then
        sSQL = "SELECT * FROM Lab1 WHERE Class='" & VBA.Trim(Combo1.Text) & "' ORDER by Equipment"
    End If
  End If
 
  Set adoConn = OpenConnection
  
  Set adoRS = adoConn.Execute(sSQL)
  If Not adoRS.BOF = False Then
    Set adoRS = Nothing 'for closing RS recordset
    Exit Sub
  End If


  With MSFlexGrid1
    Do While Not adoRS.EOF
      .Rows = .Rows + 1      'for adding row in MSFlexGrid
      .TextMatrix(.Rows - 1, 0) = .Rows - 1 & "."
      .TextMatrix(.Rows - 1, 1) = "" & adoRS.Fields("Equipment")
      .TextMatrix(.Rows - 1, 2) = "" & adoRS.Fields("no")
       adoRS.MoveNext
    Loop
  End With
  adoRS.Close
  
  Set adoRS = Nothing 'for closing RS recordset
  CloseConnection adoConn
  
  fillChkbox 168
  
End Sub


Private Sub fillChkbox(character As Integer)
  Dim lRow As Long
  
  With MSFlexGrid1
    .Col = .Cols - 1
    For lRow = .FixedRows To .Rows - 1
      .Row = lRow
      .CellFontName = "Wingdings"
      .CellFontSize = 14
      .CellAlignment = flexAlignCenterCenter
      .TextMatrix(lRow, .Col) = Chr$(character)
    Next lRow
  End With
End Sub

Private Sub MSFlexGrid1_Click()
  Dim lCol As Long, lRow As Long
  
  With MSFlexGrid1
    lRow = .MouseRow
    lCol = .MouseCol
    If lCol = .Cols - 1 And lRow >= .FixedRows Then
      If Asc(.TextMatrix(lRow, lCol)) = FLEXNO Then
        .TextMatrix(lRow, 3) = Chr$(FLEXYES)
      Else
        .TextMatrix(lRow, lCol) = Chr$(FLEXNO)
      End If
    End If
  End With
  
End Sub
The code for the module:
Code:

Private Const try1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Project1\try1.mdb;Persist Security Info=False"

Public Function OpenConnection() As ADODB.Connection
  Set OpenConnection = New ADODB.Connection
  OpenConnection.Open try1
End Function

Public Sub CloseConnection(myConn As ADODB.Connection)
  If Not myConn Is Nothing Then
    myConn.Close
    Set myConn = Nothing
  End If
End Sub
Reply With Quote
  #20  
Old 10-27-2010, 06:03 AM
ndsaid ndsaid is offline
Freshman
 
Join Date: Oct 2010
Posts: 27
Default Vb6:button for saving check box into flexgrid

Thank you so much...but I take time to understand these codes because I'm beginner in visuL bASIC 6. so sorry ......

Can u explain about these code:


Private Const FLEXYES As Integer = 254
Private Const FLEXNO As Integer = 168
Reply With Quote
Reply

Tags
checkbox, msflexgrid


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->