Date/Time to String?

This is a discussion on Date/Time to String? within the C++ Programming forums, part of the General Programming Boards category; Hey, Is there a way to get a field out of an access database which is in DateTime format to ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    4

    Date/Time to String?

    Hey,

    Is there a way to get a field out of an access database which is in DateTime format to be displayed on screen as a string? I'm using Borland C++. I've tried this but it doesnt work:

    Code:
    this->SearchLoanStartEdit->Text = searchQuery->FieldByName("rented")->AsString;
    With this code, I get a data mismatch error.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    > With this code, I get a data mismatch error.
    Paste the exact text.
    Some compilers report "can't convert <sometype> to <someothertype>"
    Knowing what the types actually are (how you declared things) might make it a lot easier to tell you how to fix it.

    Some declarations would help as well, we can't psychicly guess what you wrote.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    Here is the entire block of code. The database it is getting the info from is an MS Access database and the rented field is a Date/Time datatype.

    Code:
    void __fastcall TMainForm::SearchResultsListBoxClick(TObject *Sender)
    {
            TADOQuery *searchQuery = new TADOQuery(this);
            if(searchQuery)
            {
                    searchQuery->Connection = DataModule1->ADOConnection1;
                    if(search_type == 0)
                    {
                            searchQuery->SQL->Text = "SELECT * FROM accounts WHERE uid = "+IntToStr((int)this->SearchResultsListBox->Items->Objects[this->SearchResultsListBox->ItemIndex]);
                            searchQuery->Active = true;
                            if(searchQuery->Active && searchQuery->RecordCount > 0)
                            {
                                    this->SearchNameEdit->Text = searchQuery->FieldByName("contact_name")->AsString;
                                    this->SearchAddressEdit->Text = searchQuery->FieldByName("street")->AsString;
                                    this->SearchTownEdit->Text = searchQuery->FieldByName("town")->AsString;
                                    this->SearchCityEdit->Text = searchQuery->FieldByName("city")->AsString;
                                    this->SearchPostcodeEdit->Text = searchQuery->FieldByName("postcode")->AsString;
                                    this->SearchTelephoneEdit->Text = searchQuery->FieldByName("telephone")->AsString;
                            }
                    }
                    else if(search_type == 1)
                    {
                            searchQuery->SQL->Text = "SELECT * FROM videos WHERE uid = "+IntToStr((int)this->SearchResultsListBox->Items->Objects[this->SearchResultsListBox->ItemIndex]);
                            searchQuery->ParamCheck = false;
                            searchQuery->Active = true;
                            if(searchQuery->Active && searchQuery->RecordCount > 0)
                            {
                                    this->SearchTitleEdit->Text = searchQuery->FieldByName("title")->AsString;
                                    searchQuery->Active = false;
                                    searchQuery->SQL->Text = "SELECT Genres.* FROM Genres, Videos WHERE ((Genres.uid)=[Videos].[Genre_ID]) AND videos.uid = "+IntToStr((int)this->SearchResultsListBox->Items->Objects[this->SearchResultsListBox->ItemIndex]);
                                    searchQuery->Active = true;
                                    this->SearchGenreEdit->Text = searchQuery->FieldByName("name")->AsString;
                                    searchQuery->Active = false;
                                    searchQuery->SQL->Text = "SELECT stock.* FROM stock, Videos WHERE ((stock.uid)=[Videos].[in_stock]) AND videos.uid = "+IntToStr((int)this->SearchResultsListBox->Items->Objects[this->SearchResultsListBox->ItemIndex]);
                                    searchQuery->Active = true;
                                    this->SearchStockEdit->Text = searchQuery->FieldByName("stock_status")->AsString;
                                    searchQuery->Active = false;
                                    searchQuery->SQL->Text = "SELECT accounts.*, loans.returned FROM (loans INNER JOIN videos ON loans.video_uid = videos.uid) INNER JOIN accounts ON loans.member_uid = accounts.uid WHERE (((loans.returned)='0') AND ((accounts.uid)=[loans].[member_uid]) AND ((videos.uid)= " + IntToStr((int)this->SearchResultsListBox->Items->Objects[this->SearchResultsListBox->ItemIndex]) + "))";
                                    searchQuery->Active = true;
                                    if(searchQuery->Active && searchQuery->RecordCount > 0)
                                    {
                                            this->SearchLoanEdit->Text = searchQuery->FieldByName("contact_name")->AsString;
                                    }
                                    else
                                    {
                                            this->SearchLoanEdit->Text = "Video not on loan";
                                    }
                                    searchQuery->Active = false;
                                    searchQuery->SQL->Text = "SELECT * FROM loans WHERE video_uid = "+IntToStr((int)this->SearchResultsListBox->Items->Objects[this->SearchResultsListBox->ItemIndex]) + " AND returned = '0'";
                                    searchQuery->Active = true;
                                    if(searchQuery->Active && searchQuery->RecordCount > 0)
                                    {
                                            this->SearchLoanStartEdit->Text = searchQuery->FieldByName("rented")->AsString();
                                    }
                                    else
                                    {
                                        this->SearchLoanStartEdit->Text = "Video not on loan";
                                    }
                            }
                    }
            }
            delete searchQuery;
    }
    The error isnt a compiler error, It's a popup dialog error which says "Data type mismatch in criteria expression"

    Thanks

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    It's an SQL Error. You are passing SQL an empty string
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    I know why the error is appearing now. My code is wrong for checking if the selected field is empty or not. How can I do that in SQL?

    Thanks

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    I've fixed it now. It was IS NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21