SQLCHAR* and const char

Pages: 123456
For example, look at my SQLDumpData(SQL& sql) function, which shows how I do what you are failing. I declare
szQuery[] as a char array and copy my string to it. Then, I execute the statement against the database like so...

SQLExecDirect(hStmt,(SQLTCHAR*)szQuery,SQL_NTS)==SQL_SUCCESS

Note the cast. Hey! It works! Sometimes with ODBC and SQL you've gotta get low down mean and nasty.
Once you are connected, all it boils down to is blowing correct SQL statements at the database. This is true for creating/dropping tables, writing records with SQL INSERT or UPDATE statements, or retrieving with SELECT. However, its a little more complicated than that because SQLBindParameter() and SQLBindCol() kind of have to be dealt with. But that's tomorrow's story. Right now I'm going to bed. Goodnight!
Thank You. Goodnight!
Lamblion:

I tried to go to sleep but can't! I can't understand why SQLConfigDataSource() is failing. In that last code I posted note I changed iInstallerError to this....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
unsigned int iInstallerError()
{
 DWORD pErr;
 char szErrMsg[512];
 WORD  cbMsgBuffer=512;
 WORD  cbRet;
 WORD  wErrNum=1;

 while(SQLInstallerError(wErrNum,&pErr,szErrMsg,cbMsgBuffer,&cbRet)!=SQL_NO_DATA)
 {
  printf("wErrNum    = %d\t",wErrNum);
  printf("szErrMsg = %s\n",szErrMsg);
  wErrNum++;
 };

 return (unsigned int)pErr;
}


As you can see, its outputting szErrMsg if called (it'll be called if iCreateDB fails - and its failing for you. Could you run this again and see what szErrMsg returns to us? That might give a darn good hint what the problem is. I'd really like to know, because I've had this code going all the way back ti when I first figured ODBC iut 10+ years ago, and never had trouble with it 'till now.
Say DSTR3A:

Would it be much work/hassle for me to install MySql like you have? I really don't know much about it except that its freeware of some sort or other and its popular. I went to the web site years ago and looked at it for a couple minutes, and decided I had better things to do. If it wouldn't be too much of a hassle to get it installed and working for programatic access (I don't need too much in the way of front ends), I'd consider it. I could probably help you better then too. Also, I have a bit of a selfish reason. I'd like to get my ODBC class set up to connect and work with it. Just for bragging rights, you know!

But then, on second thought, you are now trying to connect to Access anyway, so I should be able to get your problems solved, in any case? Well, tomorrow's another day. I better go take another sleeping pill!
Last edited on
Sorry, freddie1. I had to go to bed.

It's now morning and I have replaced your old code with exactly what you last posted. I stil get this when I attempt to run it --

iCreateDB() Failed, i.e., we couldn't create the Access Database. Nicht Gut!

I will attempt to go through the code myself in a little while and see if I can detect anything.

BTW, I am compliling strictly with VS 2008, so it's not the compiler.
Actually, here is the entire error message --

1
2
3
4
5
Sql.strDBQ = C:\Programming\VS 2008\Win Console\database 01\Debug\TestData.mdb
wErrNum    = 1  szErrMsg = Driver's ConfigDSN, ConfigDriver, or ConfigTranslator
 failed
Sql.blnConnected  = -858993460
iCreateDB() Failed, i.e., we couldn't create the Access Database.  Nicht Gut!


And yes, I am running as admin.
Good Morning Freddie1! So I figure this. I have Access 2010, I'm using Visual Studio 2010. Apparently like you said I have some wrapper thing going on. Lets get real vanilla and just connect Access 2010 and do a row count on a table. Thats all I want, forget mysql and whatever else. Does that make it easier? Thank you.
Yes, DSTR3A, we'll get you working here in a bit. I believe I've just figured out the problem, (why my stuff works for me but not for Lamblion) and its related to the newer Access versions you and Lamblion are using. First let me post something for Lamblion...
That helps Lamblion. Tricky problem. I can't get it to fail no matter how many computers I try it on and you can't get
it to work nohow. I do have a theory though. It was DSTR3A's code that gave me the hint. I saw this in his code...

SQLCHAR szDSN[256] =
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\FILEBLOCK\\Fileblocker.accdb;";

I have never seen a Driver specified like this for Access...

Microsoft Access Driver (*.mdb, *.accdb)}

This is how I have always seen the Microsoft Access Driver specified...

Microsoft Access Driver (*.mdb)

That got me to thinking that there is something unique to my computer setups that are different from all yours. And I
believe what it is - is that I have many older versions of Microsoft Office And Access installed on all my systems than all
the young kids out there downloading the latest academic or express versions of Microsoft's Office and database software.
I work for a very, very large organization that maintains massive amounts of legacy software and databases. On the computer
I'm presently typing this Office 2000 is installed and Office 2007. Almost certainly you have much newer software than this.

However, this computer I'm now on is the one with Office 2007 (all my others have Office 2000 or Office XP) . I just wrote
a quick little program to use SQLDrivers() (an ODBC Function) to dump all the Driver Descriptions installed in my registry,
and sure enough this...

Microsoft Access Driver (*.mdb, *.accdb)}

...showed up in addition to this (what I'm used to)...

Microsoft Access Driver (*.mdb)

Here is the program. Why don't you run it and see what you come up with? The first time you run it you'll have to right
click up in the title bar of the console window and increase the console line buffer count to 5 or 6 hundred or so, because
it produces voluminous output....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef    UNICODE
   #define    UNICODE
#endif
#ifndef _UNICODE
   #define    _UNICODE
#endif
#include   <windows.h>
#include   <tchar.h>
#include   <cstdio>
#include   <string.h>
#include   <sql.h>                        //ODBC header file
#include   <sqlext.h>                     //ODBC header file
#include   "Strings.h"

int main()
{
 TCHAR szDriver[256], szAttributes[256];
 String* ptrAttributes;
 short iLen1,iLen2;
 SQLHENV hEnvr;
 int iCount;
 TCHAR* pCh;
 String s1;

 if(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnvr)!= SQL_ERROR)
 {
    SQLSetEnvAttr(hEnvr,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER);
    while(SQLDrivers(hEnvr,SQL_FETCH_NEXT,(SQLTCHAR*)szDriver,256,&iLen1,(SQLTCHAR*)szAttributes,256,&iLen2)!=SQL_NO_DATA)
    {
          pCh=szAttributes;
          for(int i=0;i<iLen2;i++)
          {
              if(*pCh==0)
                 *pCh=_T(',');
              pCh++;
          }
          *--pCh=0;
          s1=szAttributes;
          iCount=s1.ParseCount(_T(','));
          ptrAttributes = new String[iCount];
          s1.Parse(ptrAttributes,_T(','));
          _tprintf(_T("%s\n\n"),szDriver);
          for(int i=0;i<iCount;i++)
              _tprintf(_T("  ptrAttributes[%u] = %s\n"),i,ptrAttributes[i].lpStr());
          delete [] ptrAttributes;
          _tprintf(_T("\n\n\n"));
    };
    SQLFreeHandle(SQL_HANDLE_ENV,hEnvr);
 }
 getchar();

 return 0;
}


Ah! Craps! I just realized something! It uses my string class! Not the Std. Lib. string class. It needs mine because
mine contains complete string parsing capabilities, and the above program runs through a buffer returned by SQLDrivers
and replaces nulls with commas, then parses the comma delimited string into value - attribute pairs. Well, I'll post my string
class if you want to run it. Before I do though I'll post the output from my system that lists both driver descriptions, i.e., this...

Microsoft Access Driver (*.mdb, *.accdb)}

and this...

Microsoft Access Driver (*.mdb)

Here is the output I'm seeing from the above program run on my Dell Latitude E6510 with Win 7 32 bit. Note the two different Microsoft Access Driver descriptions...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
SQL Server

  ptrAttributes[0] = UsageCount=3
  ptrAttributes[1] = SQLLevel=1
  ptrAttributes[2] = FileUsage=0
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = ConnectFunctions=YYY
  ptrAttributes[5] = APILevel=2
  ptrAttributes[6] = CPTimeout=60



Microsoft ODBC for Oracle

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = SQLLevel=1
  ptrAttributes[2] = FileUsage=0
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = ConnectFunctions=YYY
  ptrAttributes[5] = APILevel=1
  ptrAttributes[6] = CPTimeout=120



Microsoft Access Driver (*.mdb)

  ptrAttributes[0] = UsageCount=2
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=2
  ptrAttributes[5] = FileExtns=*.mdb
  ptrAttributes[6] = SQLLevel=0



Microsoft Access-Treiber (*.mdb)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=2
  ptrAttributes[5] = FileExtns=*.mdb
  ptrAttributes[6] = SQLLevel=0



Driver do Microsoft Access (*.mdb)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=2
  ptrAttributes[5] = FileExtns=*.mdb
  ptrAttributes[6] = SQLLevel=0



Microsoft dBase Driver (*.dbf)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.dbf
  ptrAttributes[6] = *.ndx
  ptrAttributes[7] = *.mdx
  ptrAttributes[8] = SQLLevel=0



Microsoft dBase-Treiber (*.dbf)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.dbf
  ptrAttributes[6] = *.ndx
  ptrAttributes[7] = *.mdx
  ptrAttributes[8] = SQLLevel=0



Driver do Microsoft dBase (*.dbf)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.dbf
  ptrAttributes[6] = *.ndx
  ptrAttributes[7] = *.mdx
  ptrAttributes[8] = SQLLevel=0



Microsoft Excel Driver (*.xls)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.xls
  ptrAttributes[6] = SQLLevel=0



Microsoft Excel-Treiber (*.xls)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.xls
  ptrAttributes[6] = SQLLevel=0



Driver do Microsoft Excel(*.xls)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.xls
  ptrAttributes[6] = SQLLevel=0



Microsoft Paradox Driver (*.db )

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.db
  ptrAttributes[6] = SQLLevel=0



Microsoft Paradox-Treiber (*.db )

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.db
  ptrAttributes[6] = SQLLevel=0



Driver do Microsoft Paradox (*.db )

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.db
  ptrAttributes[6] = SQLLevel=0



Microsoft Text Driver (*.txt; *.csv)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.
  ptrAttributes[6] = *.asc
  ptrAttributes[7] = *.csv
  ptrAttributes[8] = *.tab
  ptrAttributes[9] = *.txt
  ptrAttributes[10] = *.csv
  ptrAttributes[11] = SQLLevel=0



Microsoft Text-Treiber (*.txt; *.csv)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.
  ptrAttributes[6] = *.asc
  ptrAttributes[7] = *.csv
  ptrAttributes[8] = *.tab
  ptrAttributes[9] = *.txt
  ptrAttributes[10] = *.csv
  ptrAttributes[11] = SQLLevel=0



Driver da Microsoft para arquivos texto (*.txt; *.csv)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.
  ptrAttributes[6] = *.asc
  ptrAttributes[7] = *.csv
  ptrAttributes[8] = *.tab
  ptrAttributes[9] = *.txt
  ptrAttributes[10] = *.csv
  ptrAttributes[11] = SQLLevel=0



Microsoft Visual FoxPro Driver

  ptrAttributes[0] = UsageCount=2
  ptrAttributes[1] = APILevel=0
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.dbc
  ptrAttributes[6] = *.dbf
  ptrAttributes[7] = SQLLevel=0



Microsoft FoxPro VFP Driver (*.dbf)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=0
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.dbf
  ptrAttributes[6] = *.cdx
  ptrAttributes[7] = *.idx
  ptrAttributes[8] = *.fpt
  ptrAttributes[9] = SQLLevel=0
.......continued

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Microsoft dBase VFP Driver (*.dbf)

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=0
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.dbf
  ptrAttributes[6] = *.cdx
  ptrAttributes[7] = *.idx
  ptrAttributes[8] = *.fpt
  ptrAttributes[9] = SQLLevel=0



Microsoft Visual FoxPro-Treiber

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=0
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.dbf
  ptrAttributes[6] = *.cdx
  ptrAttributes[7] = *.idx
  ptrAttributes[8] = *.fpt
  ptrAttributes[9] = SQLLevel=0



Driver para o Microsoft Visual FoxPro

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=0
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=1
  ptrAttributes[5] = FileExtns=*.dbf
  ptrAttributes[6] = *.cdx
  ptrAttributes[7] = *.idx
  ptrAttributes[8] = *.fpt
  ptrAttributes[9] = SQLLevel=0



Microsoft Access Driver (*.mdb, *.accdb)

  ptrAttributes[0] = UsageCount=3
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=2
  ptrAttributes[5] = FileExtns=*.mdb
  ptrAttributes[6] = *.accdb
  ptrAttributes[7] = SQLLevel=0



Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)

  ptrAttributes[0] = UsageCount=3
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=2
  ptrAttributes[5] = FileExtns=*.xls
  ptrAttributes[6] = *.xlsx
  ptrAttributes[7] =  *.xlsb
  ptrAttributes[8] = SQLLevel=0

Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)

  ptrAttributes[0] = UsageCount=3
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=2
  ptrAttributes[5] = FileExtns=*.dbf
  ptrAttributes[6] =  *.ndx
  ptrAttributes[7] =  *.mdx
  ptrAttributes[8] = SQLLevel=0



Microsoft Access Paradox Driver (*.db)

  ptrAttributes[0] = UsageCount=3
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=2
  ptrAttributes[5] = FileExtns=*.mdb
  ptrAttributes[6] = *.accdb
  ptrAttributes[7] = SQLLevel=0



Microsoft Access Text Driver (*.txt, *.csv)

  ptrAttributes[0] = UsageCount=3
  ptrAttributes[1] = APILevel=1
  ptrAttributes[2] = ConnectFunctions=YYN
  ptrAttributes[3] = DriverODBCVer=02.50
  ptrAttributes[4] = FileUsage=2
  ptrAttributes[5] = FileExtns=*.txt
  ptrAttributes[6] =  *.csv
  ptrAttributes[7] = SQLLevel=0



SQL Native Client

  ptrAttributes[0] = UsageCount=1
  ptrAttributes[1] = APILevel=2
  ptrAttributes[2] = ConnectFunctions=YYY
  ptrAttributes[3] = CPTimeout=60
  ptrAttributes[4] = DriverODBCVer=09.00
  ptrAttributes[5] = FileUsage=0
  ptrAttributes[6] = SQLLevel=1
And like I said, it was that code of DSTR3A's that clued me into the fact that there is a new Microsoft Access Driver Description out there. So what I'm saying, is the code I gave you might work if you simply replace this...

Microsoft Access Driver (*.mdb)

with this...

Microsoft Access Driver (*.mdb, *.accdb)





Well, I just tried it and it doesn't work. Infuriating.
I believe you are correct in your theory, as I am running Office 2010, however, I have replaced all instances of --

Microsoft Access Driver (*.mdb)

with...

Microsoft Access Driver (*.mdb, *.accdb)

and it still gives me the same error output as posted above.

Since I know virtually nothing about database programming, I can't imagine what it could be. And remember, I am doing this with VS2008.

I also tried it with VC++ 2010. No go.
SUCCESS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

When I looked in the directory where I have the executable I was amazed to see that the program didn't completely fail - it created TestData.mdb! That made me realize I had forgotten about changing the connection string description in Sql.cpp, i.e., my connection class. I immediately fixed it, i.e., added the *.accdb, and it worked flawlessly! We're there Lamblion! So I'll repost all the code in just a bit, and then I'll get to your problem DSTA3A. I know you've gotten a raw deal DSTR3A. This was your thread and we hi-jacked it. I'll try to make it up to you though. My only excuse is I have a fair amount of intellectual capital invested in this code, and it was infuriating me that it wasn't working for Lamblion.
Well this works. However; I am getting one error messgae, but it still works. btw. Access 2010 32bit, Win 7 32bit, Visual Studio 2010 32bit. The error, on the SELECT STATEMENT,then the code.

1 IntelliSense: a value of type "const char *" cannot be used to initialize an entity of type "SQLCHAR *"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <windows.h>
#include <stdio.h>
//#include <sql.h>
#include <sqlext.h>
//#include <odbcss.h>
//#include <string>
/* Data Access Method used in this sample */
const char* DAM = "Direct ODBC";

/* Connection string for Direct ODBC */
SQLCHAR szDSN[256] = 
    "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\FILEBLOCK\\Fileblocker.accdb;";

main()
{
    HENV    hEnv;
    HDBC    hDbc;

    /* ODBC API return status */
    SQLRETURN  rc;

    SQLSMALLINT  iConnStrLength2Ptr;
    SQLCHAR      szConnStrOut[255];
	
    
	SQLCHAR* query = "SELECT tblIP.[IPAddress], tblIP.[IPStatus], tblIP.[IPType] FROM tblIP ORDER BY tblIP.[IPAddress] ASC;";
    
    SQLCHAR         chval1[128], chval2[128], chval3[128], colName[128];
    SQLINTEGER      ret1, ret2, ret3;

    /* Number of rows and columns in result set */
    SQLINTEGER      rowCount = 0;
    SQLSMALLINT     fieldCount = 0, column = 0;
    HSTMT           hStmt;

    /* Allocate an environment handle */
    rc = SQLAllocEnv(&hEnv);
    /* Allocate a connection handle */
    rc = SQLAllocConnect(hEnv, &hDbc);

    /* Connect to the 'Northwind 2007.accdb' database */
    rc = SQLDriverConnect(hDbc, NULL, szDSN,  _countof(szDSN), 
		szConnStrOut, 255, &iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
    if (SQL_SUCCEEDED(rc)) 
    {
        printf("%s: Successfully connected to database. Data source name: \n  %s\n", 
           DAM, szConnStrOut);

        /* Prepare SQL query */
        printf("%s: SQL query:\n  %s\n", DAM, query);
		rc = SQLAllocStmt(hDbc,&hStmt);
        rc = SQLPrepare(hStmt, query, SQL_NTS);
       
        /* Bind result set columns to the local buffers */ 
        rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, chval1, 128, &ret1);
        rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval2, 128, &ret2);
		rc = SQLBindCol(hStmt, 3, SQL_C_CHAR, chval3, 128, &ret3);

        /* Execute the query and create a record set */
        rc = SQLExecute(hStmt); 
        if (SQL_SUCCEEDED(rc)) 
        {
            printf("%s: Retrieve schema info for the given result set:\n", DAM);
            SQLNumResultCols(hStmt, &fieldCount);
            if (fieldCount > 0)
            {
                for (column = 1; column <= fieldCount; column++)
                {
                    SQLDescribeCol(hStmt, column,
                        colName, sizeof(colName), 0, 0, 0, 0, 0);
                    printf(" | %s", colName);    
                }
                printf("\n");
            }
            else
            {
                printf("%s: Error: Number of fields in the result set is 0.\n", DAM);
            }

            printf("%s: Fetch the actual data:\n", DAM);
            /* Loop through the rows in the result set */
            rc = SQLFetch(hStmt);
            while (SQL_SUCCEEDED(rc)) 
            {
                printf(" | %s | %s\n", chval1, chval2, chval3);
                rc = SQLFetch(hStmt);
                rowCount++;
            };

            printf("%s: Total Row Count: %d\n", DAM, rowCount);
            rc = SQLFreeStmt(hStmt, SQL_DROP);
        }
    }
    else
    {
        printf("%s: Couldn't connect to %s.\n", DAM, szDSN);
    }

    /* Disconnect and free up allocated handles */
    SQLDisconnect(hDbc);
    SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
    SQLFreeHandle(SQL_HANDLE_ENV, hEnv);

    printf("%s: Cleanup. Done.\n", DAM);
}
Last edited on
Congratulations Freddie1!
Lamblion wrote...


I believe you are correct in your theory, as I am running Office 2010, however, I have replaced all instances of --

Microsoft Access Driver (*.mdb)

with...

Microsoft Access Driver (*.mdb, *.accdb)

and it still gives me the same error output as posted above.


In Sql.cpp in SQL::MakeConnectionString() you need to change the elseif about mid way down...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void SQL::MakeConnectionString(void)
{
 if(strDriver=="SQL Server")
 {
    if(strDBQ=="")
    {
       strConnectionString="DRIVER=";
       strConnectionString=strConnectionString+strDriver+";"+"SERVER="+strServer+";";
    }
    else
    {
       strConnectionString="DRIVER=";
       strConnectionString=strConnectionString+strDriver+";"+"SERVER="+strServer+";"+ \
       "DATABASE=" + strDatabase + ";" + "DBQ=" + strDBQ + ";";
    }
 }
 else if(strDriver=="Microsoft Access Driver (*.mdb, *.accdb)")  // <<<<< here! add , *.accdb
 {
    strConnectionString="DRIVER=";
    strConnectionString=strConnectionString+strDriver+";"+"DBQ="+strDBQ+";";
 }
 else if(strDriver=="Microsoft Excel Driver (*.xls)")
 {
    strConnectionString="DRIVER=";
    strConnectionString=strConnectionString+strDriver+";"+"DBQ="+strDBQ+";";
 }
}


Also, check your directory to make sure it didn't create the database. That's what happened to me when I realized the Sql.cpp thingie above. If it created the DB delete it and run again after you've fixed Sql.cpp
Last edited on
Say DSTR3A, I'm finally ready to focus on your problem, that is, if you have'nt figured it out already. I'm seeing a lot of ancient and deprecated functions you are using, i.e., SqlAllocEnv(), SQLAllocConnect(), etc.

So we are on the same page and working with the same database, would you care to work with Microsoft's Northwind database? If you like, I'll write a program showing how to dump some records out of it or put new ones in. You choose the table. First maybe I ought to make sure I indeed do have that DB.
Pages: 123456