Results 1 to 6 of 6

Thread: Data environment question, incrementing a recordset [*Not Resolved*]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Data environment question, incrementing a recordset [*Not Resolved*]

    Hello

    I am using the datagrid and data environment. But my sql query does not work. Can anyone tell me what l am doing wrong from the code below.

    Code:
    Private Sub cmdNext_Click() 
    Dim strSQL As String 
    Dim monthName As String 
    
    'increment the months to display each month in the year 
    deSalaries.rscmdIncrementMonths.MoveNext 
    If deSalaries.rscmdIncrementMonths.EOF = True Then 
        deSalaries.rscmdIncrementMonths.MoveLast 
    End If 
                                                                                                                                                                        
    monthName = txtMonths 'Get the current month that is display and convert it to a date format 
    monthName = (CDate(monthName)) 
    
    'Write out the sql query to find all the employees under the current month 
    strSQL = "SELECT `Month`.`MonthName`, " & _ 
                "LinkMonthEmployee.EmployeeNo, " & _ 
                "LinkMonthEmployee.Surname, " & _ 
                "LinkMonthEmployee.AccountNo, " & _ 
                "LinkMonthEmployee.EmployeeSalary, " & _ 
                "LinkMonthEmployee.Position, " & _ 
                "LinkMonthEmployee.Certificate, " & _ 
                "LinkMonthEmployee.GrossSalary, " & _ 
                "LinkMonthEmployee.Tax, " & _ 
                "LinkMonthEmployee.SocialSecurity, " & _ 
                "LinkMonthEmployee.NetSalary " & _ 
            "FROM Month, LinkMonthEmployee " & _ 
            "WHERE `Month`.MonthID = LinkMonthEmployee.MonthID AND Month`.MonthID = '" & monthName & "' " 
    
    'Set all the properties for the data environment 
    deSalaries.Commands("cmdAllDetails").CommandType = adCmdText 
    deSalaries.Commands("cmdAllDetails").CommandText = strSQL 
    dgrSalaries.DataMember = "cmdAllDetails" 'Set the datamember to cmdAddDetails 
    dgrSalaries.ReBind 'Rebind the datagrid to display all the employees under the current month that has been incremented 
    
    end sub
    Not sure where l am going wrong with the above code, but it will not work but compiles ok. Not sure if l have set all the properties in the data environment. Any help will be most gratefull in solving this problem for me.

    Many thanks in advance

    Steve
    Last edited by steve_rm; Mar 13th, 2004 at 10:41 AM.
    steve

  2. #2
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381

    Re: Data environment question, incrementing a recordset

    What is a sample value for your txtMonth and monthName?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331
    Hello

    I think l know what the problem is.

    MonthName in the database is a text database, so l can display the date like JAN 04, FEB 04 etc. The monthID is a date/time datatype, 1/01/04, 1/02/04.

    So when the SQL statement compares both these dates, they are false.

    How can l change the format of the monthName to display like JAN 04 to 1/01/04, as what it is in the database.

    Many thanks for your help

    Steve
    steve

  4. #4
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    How can l change the format of the monthName to display like JAN 04 to 1/01/04, as what it is in the database.
    For example, if your txtMonth = "JAN 04", you need to tweak this so that VB would know that the "04" is the year and not the day.

    VB Code:
    1. Dim monthName as String
    2. monthName = FORMAT(CDATE(LEFT(txtMonth,4) & "20" & RIGHT(txtMonth,2)),"M/dd/yyyy")
    3.  
    4. 'LEFT(txtMonth,4) & "20" & RIGHT(txtMonth,2)                             will give you "JAN 2004"
    5. 'CDATE(LEFT(txtMonth,4) & "20" & RIGHT(txtMonth,2))                      will give you "1/1/2004"
    6. 'FORMAT(CDATE(LEFT(txtMonth,4) & "20" & RIGHT(txtMonth,2)),"M/dd/yyyy")  will give you "1/01/2004"

    Since you're data type in the database is date/time, the value can have something like this "2004-01-01 09:01:00.000". So even if your date is a match, when you try to look for it in the database, you will not get a record match because of the "time" especially in SQL server.

    What I do is I format my date to a "MM/dd/yyyy" so in this case, it would be like:

    VB Code:
    1. monthName = FORMAT(CDATE(LEFT(txtMonth,4) & "20" & RIGHT(txtMonth,2)),"MM/dd/yyyy")   'Will give me 01/01/2004

    Then on the databse side I will convert my datetime type into the same format.
    VB Code:
    1. ... AND CONVERT(CHAR(10),[Month].MonthID,101) = '" & monthName & "' "

    The query will the give you an exact match.

    On a side note, you should stay away from this type of coding.
    VB Code:
    1. Dim monthName As String
    2. .
    3. .
    4. monthName = txtMonths 'Get the current month that is display and convert it to a date format
    5. monthName = (CDate(monthName))  '<--- NOTE

    Your monthName is a string variable and you are passing CDATE into it. CDATE converts a string into a date type so it would not make sense to pass this back to a string variable.

    Although VB6 would accept this, VB.NET would crap this out. These are some of the issues VB.NET addressed with the old VBs hence, trouble shooting error in the code was minimized because they catch it before it happens.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331
    Hello

    Many thanks for all your help with helping me solve this problem, but l am still experiencing problems with this. i have followed all your instructions, and still can't understand where l am going wrong with it.

    I have sent a small version of my program. I hope someone can understand it and finally solve the problem that l am having with it.

    Many thanks for your help, it is most grateful

    Steve
    Attached Files Attached Files
    steve

  6. #6
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    I have sent a small version of my program
    I'm again assuming that this is a small part of your whole project. I browsed through it and the database you sent with it. The monthID field of your table is of a "text" type and not date/time as you impled. I guess you are reading and displaying the recordset during the form_load and the grid is a binded grid to your database coz I couldn't see initialization of "deEmployees" object .

    I debugged the cmdNext_Click routine and the only thing I see that needs to be replaced is the one below.

    Replace this....
    VB Code:
    1. "WHERE Month.MonthID = LinkMonthEmployee.MonthID AND Month.MonthID = #" & monthName & "# "


    With this...
    VB Code:
    1. "WHERE Month.MonthID = LinkMonthEmployee.MonthID AND Month.MonthID = '" & monthName & "' "


    Also, this should be on the "VB6" forum and not on the "Reports". There would be a lot more people who would be able to help you troubleshoot your problems if posted correctly.

    Hope this helps.
    Last edited by ARPRINCE; Mar 15th, 2004 at 09:08 AM.

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