Date in VB.NET vs. MSAccess and SELECT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bixfeldt
    New Member
    • Nov 2008
    • 9

    Date in VB.NET vs. MSAccess and SELECT

    I have a calendar on a simple windows form and when I choose a date it will put the date into a textbox with the format YYYY-MM-DD

    When I choose a date i look up in a MSAccess database if there is any appointments for the selected date.

    In the file (where the database query is in) calendar.xsd i can use the query below and all my appontments is listed in my datagrid.
    <CommandText>SE LECT * FROM tbNotification</CommandText>

    But If I want to use the selected date that I have in my textbox, i get an error saying that "There is some values or parameters missing" (a simple translation from Swedish, don´t know the exact English translation). The Query I try to use is the one below:
    <CommandText>SE LECT * FROM tbNotification WHERE ([Date] = '#'+txtSelDate+ '#')</CommandText>

    I have tried:
    - ALOT of syntaxes in the query string for the ## around the date
    - putting the date as String, as Date etc etc
    - putting the date as a variable (selDate = txtSelDate.text )

    I have now tried everything I can think of. Does anyone of you guys a solution for my problem?

    I'm extremely thankful for any help!

    Have a nice easter! :)
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    Hello bixfeldt,

    In your Access database how is the field “Date” being stored? Text or Date/time?

    CroCrew~

    Comment

    • bixfeldt
      New Member
      • Nov 2008
      • 9

      #3
      Have tried both

      I have tried both Date/Time and Text but for the moment I store it as Text, not in Date/Time format.

      Comment

      • CroCrew
        Recognized Expert Contributor
        • Jan 2008
        • 564

        #4
        Well the correct way is to store it as a Date/Time. Also “Date” is a reserved word and should not be used as your field name. Be clever with your field names. Like “NotificationDa te”.

        With that said let us know how you’re going to store it and the name of the field and we will try to provide you with your answer. Also, if you are going to store the data as a “text” give us some examples of what you have stored.

        Again, I personally would not store dates as text.

        CroCrew~

        Comment

        • bixfeldt
          New Member
          • Nov 2008
          • 9

          #5
          Hi,
          Thanks for trying to help me! I have now renamed the column name from Date to notificationDat e to avoid any problems with reserved words. I have also changed it to Date/Time instead of Text. The correct date format for Sweden is yyyy-mm-dd, so I save the dates as this into the database, for example 2010-01-01.

          One "problem" that may be involved is that I have to use the character # before and after the date when working toward an Access database.

          I hope this information is understandable. :)

          Thanks in advance for you help!

          Comment

          • bixfeldt
            New Member
            • Nov 2008
            • 9

            #6
            I forgott to include the code below:
            In the Form_Onload event in frmCalendar I can use the code below to show all entries in the database:

            <!-- --------------------------------------------------------------------------------------------------- -->
            txtSelDate.Text = MonthCalendar1. SelectionStart
            lblCalMonth.Tex t = DatePart(DateIn terval.Month, MonthCalendar1. SelectionStart)
            lblCalDay.Text = DatePart(DateIn terval.Day, MonthCalendar1. SelectionStart)
            selDate = FormatDateTime( txtSelDate.Text , DateFormat.Shor tDate)
            txtSelDate.Text = MonthCalendar1. TodayDate
            selDate = FormatDateTime( txtSelDate.Text , DateFormat.Shor tDate)
            selDate = "#" & selDate & "#"
            lblCalMonth.Tex t = DatePart(DateIn terval.Month, MonthCalendar1. TodayDate)
            lblCalDay.Text = DatePart(DateIn terval.Day, MonthCalendar1. TodayDate)
            Me.TbNotificati onTableAdapter. Fill(Me.Calenda rDataSet.tbNoti fication)
            <!-- --------------------------------------------------------------------------------------------------- -->

            And the SELECT statement in the calendarDataSet .xsd is as below:

            <!-- --------------------------------------------------------------------------------------------------- -->
            <SelectComman d>
            <DbCommand CommandType="Te xt" ModifiedByUser= "false">
            <CommandText>SE LECT ID, NotificationDat e, Notification, SE, [NO], DK, FI, PL, HK FROM tbNotification</CommandText>
            <Parameters />
            </DbCommand>
            </SelectCommand>
            <!-- --------------------------------------------------------------------------------------------------- -->

            The thing that I want to do is to replace the code above, in the calendarDataSet .xsd, with this one:

            <!-- --------------------------------------------------------------------------------------------------- -->
            <SelectComman d>
            <DbCommand CommandType="Te xt" ModifiedByUser= "false">
            <CommandText>SE LECT ID, NotificationDat e, Notification, SE, [NO], DK, FI, PL, HK FROM tbNotification WHERE NotificationDat e = '"#"'+selDate+" #"'</CommandText>
            <Parameters />
            </DbCommand>
            </SelectCommand>
            <!-- --------------------------------------------------------------------------------------------------- -->

            I hope this also makes it more possible to locate the problem. Thank you very much for your kind help!

            Comment

            • CroCrew
              Recognized Expert Contributor
              • Jan 2008
              • 564

              #7
              As the wise Tlhintoq says:

              TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

              Thanks,
              CroCrew~

              Comment

              • CroCrew
                Recognized Expert Contributor
                • Jan 2008
                • 564

                #8
                Hello bixfeldt,

                Within your Access database can you tell me how the “NotificationDa te” data looks when you run “SELECT NotificationDat e FROM tbNotification” . I know you want it to display in a Sweden format “yyyy-mm-dd” through your application but within your Access database when you run the query how does it look? “mm-dd-yyyy” or “yyyy-mm-dd”

                CroCrew~

                Comment

                • bixfeldt
                  New Member
                  • Nov 2008
                  • 9

                  #9
                  Hi,
                  God tip, with the wrap code (#)!
                  If I look into my Access database, it has the format "yyyy-mm-dd".

                  // Bixfeldt

                  Comment

                  • bixfeldt
                    New Member
                    • Nov 2008
                    • 9

                    #10
                    One more thing that might be a problem:
                    In my .xsd-file, wich is a XML-file, I have the line
                    Code:
                    SELECT ID, NotificationDate, Notification, SE, [NO], DK, FI, PL, HK FROM tbNotification WHERE NotificationDate = selDate
                    selDate is declared under Form_Load in Calender.vb. Can i reference to a declared value in a form from an XML-file?

                    Can this be the problem?

                    Comment

                    • CroCrew
                      Recognized Expert Contributor
                      • Jan 2008
                      • 564

                      #11
                      Hello bixfeldt,

                      Ok, with the changes you have made (changing the field name from “Date” to “NotificationDa te” and changing the field type from “Text” to “Date/Time”) your SQL query should be:

                      SELECT * FROM tbNotification WHERE (NotificationDa te = #1970-01-09#)

                      Don’t wrap the date with single quotes.

                      Hope this helps,
                      CroCrew~

                      Comment

                      • bixfeldt
                        New Member
                        • Nov 2008
                        • 9

                        #12
                        Yes, that works. But i don´t want to put a static date value in the code, but pick it up from my textbox called txtselDate.

                        How is the syntax to use the value that I have in a textbox at the design view called frmCalendar. I have today the following code at the form page:
                        Code:
                        selDate = txtSelDate.Text
                        Then, the value above, I want to use in the SQL statement. It is this SQL-statement that I might belive is wrong:
                        Code:
                        SELECT * FROM tbNotification WHERE (NotificationDate = #+ selDate +#
                        So, the problem is that I don't wabt to use a static value, but instead use the value that is given in the textbox. Do you understand what i mean?

                        Comment

                        • CroCrew
                          Recognized Expert Contributor
                          • Jan 2008
                          • 564

                          #13
                          Hello bixfeldt,

                          Your SQL query should be a string that you’re building.

                          Example:
                          “SELECT * FROM tbNotification WHERE (NotificationDa te = #” & selDate & “#”

                          Disclaimer! This is an example. You should always use parameters to protect yourself from SQL injection attacks.

                          Hope this helps,
                          CroCrew~

                          Comment

                          • bixfeldt
                            New Member
                            • Nov 2008
                            • 9

                            #14
                            It doesn´t work. I get the error messages below:
                            Character ' ', hexadecimal values 0x20 is illegal in an XML name
                            It seems that the character "&" is not allowed in an XML-file.

                            I want to remind of that the SQL statement is not in the code-behind vb-file but in the file called calendarDataSet .xsd that is an XML-file that Visual studio automatically creates when adding a dataset, binding source and table adapter. Visual Studio automatically creates SELECT, INSERT, UPDATE, DELETE commands and it is this commands that I try to manipulate because I just want to see the notification for a specific date, not all notifications in the database.

                            Comment

                            • bixfeldt
                              New Member
                              • Nov 2008
                              • 9

                              #15
                              Isn't there any simple solution where I can put an SQL command in the code-behind for each button instead of using an XML file with the SQL statements? Do you have a better solution than using XML?

                              Comment

                              Working...