|
i was trying this code....
But for the select statement you need a table name. The excel sheet has multiple tables but does not have a name.So is there any programatic way of giving table name to the tables in the excel sheet...
or is there any other way of getting the data. i just want to fetch the data and manipulate on it.
void CReadExcelDlg::OnButton1()
{
CDatabase database;
CString sSql;
CString sItem1, sItem2;
CString sDriver;
CString sDsn;
CString sFile = "ReadExcel.xls"; // the file name. Could also be something
// like C:\\Sheets\\WhatDoIKnow.xls
// Clear the contents of the listbox
m_ctrlList.ResetContent();
// Retrieve the name of the Excel driver. This is
// necessary because Microsoft tends to use language
// specific names like "Microsoft Excel Driver (*.xls)" versus
// "Microsoft Excel Treiber (*.xls)"
sDriver = GetExcelDriver();
if (sDriver.IsEmpty())
{
// Blast! We didn´t find that driver!
AfxMessageBox("No Excel ODBC driver found");
return;
}
// Create a pseudo DSN including the name of the Driver and the Excel file
// so we don´t have to have an explicit DSN installed in our ODBC admin
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s", sDriver, sFile);
TRY
{
// Open the database using the former created pseudo DSN
database.Open(NULL, false, false, sDsn);
// Allocate the recordset
CRecordset recset(&database);
// Build the SQL string
// Remember to name a section of data in the Excel sheet using
// "Insert->Names" to be able to work with the data like you would
// with a table in a "real" database. There may be more than one table
// contained in a worksheet.
sSql = "SELECT field_1, field_2 "
"FROM demo_table "
"ORDER BY field_1";
// Execute that query (implicitly by opening the recordset)
recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
// Browse the result
while (!recset.IsEOF())
{
// Read the result line
recset.GetFieldValue("field_1", sItem1);
recset.GetFieldValue("field_2", sItem2);
// Insert result into the list
m_ctrlList.AddString(sItem1 + " --> "+sItem2);
// Skip to the next resultline
recset.MoveNext();
}
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// A database exception occured. Pop out the details...
AfxMessageBox("Database error: " + e->m_strError);
}
END_CATCH;
}
// Get the name of the Excel-ODBC driver
// Contibuted by Christopher W. Backen - Thanx Christoper
CString CReadExcelDlg::GetExcelDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
// Get the names of the installed drivers
// ("odbcinst.h" has to be included )
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";
// Search for the driver...
do
{
if (strstr(pszBuf, "Excel") != 0)
{
// Found !
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, '\0') + 1;
}
while (pszBuf[1] != '\0');
return sDriver;
}
|
|
|
|
|
If you are creating the excel sheet with the Application provided by me, it will give the name to your sheet as you desire.
If you are using manually created excel sheet, You need to provide the name to the table of the sheet.
To do that, Select the columns to name or Select all your text including header.
In Insert Menu -> Name -> Define, You can give the name to your sheet.
I am using MS excel 2003. I don't know whether it is supported in lower versions. Please check it.
Anurag Gandhi.
http://www.softgandhi.co.nr
|
|
|
|
|
|
Hi,
I am facing a peculiar problem with vectors. I define a vector in my program with a size given by another variable. Works fine.
But the same thing, I try inside a structure gives me a copiler error of C2061: sytax error : identifier size
Code:
#include <vector>
void main()
{
int size =4;
vector<int> vect(size);
vect[0] = 3;
vect[1] = 2;
vect[2] = 1;
display(vect);
}
This works fine But below code doesnt
int gsize =3;
struct VectorStruct
{
vector<int> vect2(gsize); //error C2061: syntax error
// : identifier 'gsize'
};
and even if i try to give a vlaue instead of variable it fails
struct VectorStruct
{
vector<int> vect2(2); //error C2059: syntax error : 'constant'
};
Can someone help me out. I cant figure out what going wrong
Vinod
|
|
|
|
|
Please revise your post: it's not readable. We can't see any of the "<" or ">" symbols or what's between them.
Steve
|
|
|
|
|
You pass parameters to constructors of member variables in the class constructor, like:
struct VectorStruct
{
vector vect2;
VectorStruct() : vect2(2) {}
};
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Revised Post
Hi,
I am facing a peculiar problem with vectors. I define a vector in my program with a size given by another variable. Works fine.
But the same thing, I try inside a structure gives me a copiler error of C2061: sytax error : identifier size
Code:
#include
void main()
{
int size =4;
vector <int > vect(size);
vect[0] = 3;
vect[1] = 2;
vect[2] = 1;
display(vect);
}
This works fine But below code doesnt
int gsize =3;
struct VectorStruct
{
vector <int > vect2(gsize); //error C2061: syntax error
// : identifier 'gsize'
};
and even if i try to give a vlaue instead of variable it fails
struct VectorStruct
{
vector <int > vect2(2); //error C2059: syntax error : 'constant'
};
Can someone help me out. I cant figure out what going wrong
Thanks Peter for the suggestion, it works. Thanks a ton
Vinod
|
|
|
|
|
If I want the same thing to be in constructor of that structure what is the way
I tried..
int gsize =3;
struct VectorStruct
{
int size;
vector <int> vect2;
//VectorStruct():vect2(i){}; //this works
VectorStruct()
{ //constructor
size=gsize;
vect2(size);
}
}
This gives an compiler error,(error C2064: term does not evaluate to a function taking 1 arguments) howto give size in constructor? Any pointers..
|
|
|
|
|
To pass a parameter to the constructor of a member, you need to pass that parameter to the constructor of the class, e.g.
struct VectorStruct
{
vector <int> vect2;
VectorStruct(int size):vect2(size){}
}
use class:
int gsize =3;
VectorStruct vs(gsize);
VectorStruct vs1(6);
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
I am having trouble printing the value I just keyed in. However I am able to get the ascii value corresponding to it. Now how do I change that to text?
The code below does not work...
case WM_CHAR: {<br />
LPSTR s; s = (LPSTR) wParam; MessageBox(hwnd,s,"hi",MB_OK);<br />
}<br />
break;
|
|
|
|
|
// Try something like this...
case WM_CHAR: {
char s[2]={0};
s[0]=wParam;
::MessageBox(hwnd,s,"hi",MB_OK);
}
break;
|
|
|
|
|
Now how can I keep compounding all those keystrokes into a string variable...? I've tried strcat() - does not work!
|
|
|
|
|
Since s[0] = wParam will always add to the first position in the array, replace the 0 with an actual variable.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Are you using MFC? If so, using a CString to concatenate is the simplest approach. Not the most efficient but the easiest to implement.
It's += operator handles TCHAR and LPCTSTR as well as other CString references.
CString is really the only reason I ever started using MFC in the first place.
|
|
|
|
|
Hello everyone,
There is error message when executing my program,
Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access violation reading location 0x00000002.
It is very simple, does anyone know what is wrong with the program?
I have tested that when changing from extern int* p_int to extern int p_int[16], my program is ok. But I think the two statements should be the same, right?
foo.c
<br />
int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};<br />
goo.c
<br />
extern int* p_int;<br />
<br />
int main (int argc, char** argv)<br />
{<br />
int i;<br />
int sum = 0;<br />
for (i = 0; i < 16; i++)<br />
{<br />
sum += p_int [i];
}<br />
<br />
return 0;<br />
}<br />
thanks in advance,
George
|
|
|
|
|
What is the address of p_int (in main()) for both the cases.
|
|
|
|
|
Both are 0x00427b40.
regards,
George
|
|
|
|
|
George_George wrote: I have tested that when changing from extern int* p_int to extern int p_int[16], my program is ok. But I think the two statements should be the same, right?
Not quite. The compiler can't make the connection that the extern *p_int in goo.c is the same variable as the int p_int[16] in foo.c. It can't reconcile the integer pointer and the interger array. Just use extern int p_int[] in goo.c - you don't need to specify the size but you do need to tell the compiler the external variable is an array.
Judy
|
|
|
|
|
Thanks for your advice, Judy!
regards,
George
|
|
|
|
|
Hi all. I have a TCP program that crashes when a connection isnt made. One example is when i open it and dont allow the connection through a firewall it crashes in Windows. How would i go about making it try to reconnect every so often if a connection isnt established without crashing? Thanx in advance!
|
|
|
|
|
dellthinker wrote: How would i go about making it try to reconnect every so often if a connection isnt established without crashing?
Probably the first step is to use the debugger to determine exactly why it is crashing. When it crashes, look at the call stack window, see how it got there, inspect the variables.
|
|
|
|
|
I ran the debugger and it pointed at this line:
<br />
structSock.sin_addr.s_addr = *((unsigned long*)ServerAddr->h_addr_list[0]);<br />
Do i need to loop this every so often to make sure the program doesnt crash when trying to connect?
This was the error message that VC++ gave me.
<quote>
Unhandled exception in Program.exe: 0xC0000005: Access Violation.
P.S. I set a breakpoint at that line and yes it is the problem. How can i resolve this?
Anyone care to shed some light on what that means?
|
|
|
|
|
Hello,
I am currently working on a notepad-like application and I am having trouble with the CFileDialog class. In my code there is a save function, and when the user selects save as from the menu, the function is run and the "Save File As" dialog comes up. When the user enters the filename and presses the save button, my program cannot seem to get the file extension...As an example, if the user entered foo.txt for the filename, I can use GetFileExt() to get the file extension, but if the user just enters foo and selects .txt extension from the dropdown, I cannot seem to get the extension
Currently I'm using a VC++ 6.0 compiler. Here is the code:
void CMFCTestDlg::OnFileSaveas()
{
CFileDialog filesave(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Text files (*.txt)|*.txt|All Files|*||");
if (filesave.DoModal()==IDOK){
filename=filesave.GetFileTitle();
filename+=".";
filename+=filesave.GetFileExt();
CFile theFile;
theFile.Open(filename,CFile::modeWrite | CFile::modeCreate);
int filesize=m_editbox.GetWindowTextLength();
char* buffer=new char [filesize+1];
m_editbox.GetWindowText(buffer,filesize+1);
theFile.Write(buffer,filesize+1);
delete [] buffer;
theFile.Close();
m_editbox.SetModify(FALSE);
}
}
BTW, m_editbox is my textbox. The function saves the text in m_editbox to a file.
Thanks !
|
|
|
|
|
The nFilterIndex member in the OPENFILENAME structure indicates the currently selected index in the "save as type" combobox. So can get the extesnion using this index
CString csExt = dlg.GetFileExt();
if( csExt.IsEmpty())
{
int n = filesave.m_ofn.nFilterIndex
// now if n == 1, the item is "Text files (*.txt)"
// if n == 2, the items is "All Files"
}
BTW I would like to know what will you put extesnion when user selects "All files".
FYI :- Normally there will not be "All files" options in the save as dialog box.
|
|
|
|
|
LOL, thanks for pointing that out! I just copied the save stuff over from the open function, so I didn't notice...;P
And now I have a new problem...how do you associate a file extension with a particular program?!
|
|
|
|