Solved

Dbase Numeric Primary Key Search

Posted on 2009-04-11
10
651 Views
Last Modified: 2013-11-24
I am able to query a DBase table with every field except the first field which is both numeric and the primary key.  When I involve this field in any select query (in the where clause) the records returned are zero.  This is not an issue of NULL (none of them are null).  I found an almost exact question and answer in the following link but the answer was basically sucks-to-be-you if you are trying to query that field.  Unfortunatly, this is a database table for our accounting software so I have no control over the table stucture and there is no other way to select (or, more specifically, do an update to a single record) that ensures that you are getting a unique record.  Is there any workaround to this?  Any ideas?  I have made sure my Microsoft Dbase drivers are the most up to date and still no luck...    Previous question:  http://www.experts-exchange.com/Microsoft/Applications/FoxPro/Q_20912375.html

Any and all help is appreciated...  I have been banging my head against this for about 12 hours...
0
Comment
Question by:brandonmonahan
  • 6
  • 4
10 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 24125472
First of all we don't know what dBase version uses your accounting system. Second, did you try to quesry this table directly from dBase or FoxPro? Third, could you please attach some sample data, like 5 records from your table on which we could reproduce this behaviour? Fourth, did you try to use Microsoft Visual FoxPro ODBC driver instead of dBase?
0
 

Author Comment

by:brandonmonahan
ID: 24126011
1.  Dbase III
2.  No.  I don't have Fox Pro or Dbase - just drivers to query against them.  I have done the query through the filter system of DBF Viewer - which did work without difficulty.  
3. Good thought on that.  I am going to attach a table that I am having trouble with.  In the table, I am unable to query on the OETLTRNUM column.  PLEASE NOTE - It is not an XLS File - I just changed the extension so I could upload it.  You will need to change the extension back to DBF
4. No - I hadn't - but I have tried 3 different drivers now with the same results.

I want to stress that the code work excellent - and I am able to query on any column but the first - once I do that it returns no rows.

I did put some code below.  Because I use the data connection in many places I have made it a function - so I had to put stuff back-together.  The function is easy - you pass it SQL - it passes back an Array.

Thank you for your help.
Dim strCougarConnection
 
strCougarConnection = "Driver={Microsoft dBase Driver (*.dbf)};DriverID=277;DBQ=C:\CMSWIN\SCS;"
 
    Dim cxn As New ADODB.Connection
    Dim cmd As New ADODB.Command
    Dim rst As New Recordset
    Dim arrString() As Variant
 
    If conn = "Cougar" Then
        cxn.ConnectionString = strCougarConnection
    ElseIf conn = "Web" Then
        cxn.ConnectionString = strWebConnection
    End If
    
    cxn.Open
    
    cmd.ActiveConnection = cxn
 
    cmd.CommandText = SQL
    Set rst = cmd.Execute
        If returnResults Then
            If Not rst.EOF Then
                arrString() = rst.GetRows
            End If
            processSQL = arrString
        End If
    cxn.Close
    Set rst = Nothing

Open in new window

SCSOL---.XLS
0
 

Author Comment

by:brandonmonahan
ID: 24126184
I forgot the value of the SQL variable...  Just for reference - the end result will look something like:

Select OETLTRNUM, OETLDETAIL, OETLSTK, OETLDES, OETLOQTY, OETLPRICE, OETLBNUM from "SCSOL$$$.DBF" where OETLBNUM = 'AAW' and OETLTRNUM = 3
SQL = "Select OETLTRNUM, OETLDETAIL, OETLSTK, OETLDES, OETLOQTY, OETLPRICE, OETLBNUM from ""SCSOL$$$.DBF"" where OETLBNUM = '" & batch & "' and OETLTRNUM = " & MainID

Open in new window

0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 24126370
This file is not dBase III file because it contains "structural index file" bit set to 1. Either it is dBase IV or FoxPro DBF but it should not affect results. Unfortunately, you did not provide the index file, so my tests are not complete. I've created index on the first numeric column by Visual FoxPro.

I've opened your file (renamed to SCSOL.DBF) using the Microsoft dBase driver and executed several queries, like "select * from SCSOL WHERE oetltrnum = 1" etc. And results were correct... Everything worked even without index file.

All SQL commands were executed via ODBC connections established from Visual FoxPro. So the only possibility is the index which is probably not compatible to your ODBC driver.

Options are:
1) Look at your ODBC driver version (my ODBC supports even dBase V and its version is 6.00.6001.18000)
2) Copy your DBF data tables to another folder without index files and try it again

I've found one more driver - Microsoft Access dBase driver which works fine also.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24126376
Your last SELECT example retrieved 3 records.
0
 

Author Comment

by:brandonmonahan
ID: 24126708
Excellent - I think you got it!  I moved it into a seperate folder than the index file and the query worked just fine returning records.  Sorry about the version - that is just what DBF viewer told me :).  Anyway, here is the index file (rename back to MDX).  These indexes are generated by the accounting program (Cougar Mountain) so I don't have control of them.  The question I have, is there anything I can do programatically to make it work?  If I have to, I can copy DBF out into a new folder and then copy it back in after doing the selects and edits but that is a bit silly.  Either way - you have got it.  Thank you so much for your help - and happy Easter!
P.S. - In this table, returning multiple records for that query is fine - it is that the OETLTRNUM the unique tie between it and another table.  BTW - I have this same problem on all my tables I am trying to query against the first numeric column.
SCSOL---.XLS
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24126803
MDX means dBase (IV or V) files. You don't need index file for SELECTs but when you update data, index must be updated also. If you don't update index then your accounting program would crash or calculate wrong numbers without any warning...

The problem is most probably in ODBC driver which is not able to process MDX index files (selected records are wrong for me also - instead OETLTRNUM = 3 it returns 1 and 2).

One possibility exists: If you update DBF data outside the accounting application (which is always dangerous!) then you have to REINDEX these data before their next use. Cougar Mountain should have some Data maintenance functionality and reindexing should be one of possible options (other options are pack and probably data archive).

Instead of copying DBF files to another folder you may simply rename appropriate MDX file and work with DBFs directly, then rename index file back to MDX and reindex in accounting app. But, again, you have to do a backup first.

Reindexing is not necessary if you don't update key columns. But do you know all columns which are not used for indexing?
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24126829
FYI, I've been looking at index expressions in your MDX:

1) UPPER(oetlbnum)+STR(oetltrnum)+STR(oetldetail)
2) STR(oetlltype)+UPPER(oetlbnum)+STR(oetltrnum)+STR(oetldetail)
3) UPPER(oetlbnum)+UPPER(oetlstk)
4) UPPER(oetlbnum)+UPPER(oetldes)

Thank you, there was new observation for me in your question: "Microsoft dBase ODBC driver does not support MDX indexes".

And now really Happy Easter.
0
 

Author Closing Comment

by:brandonmonahan
ID: 31569265
Thank you for your help.  This will get me past this hurdle.  Great responses...  My guess now is that the index does not match the DBF - I will run the Reindex through Cougar and then check - that is probably why I was getting no rows.  Thanks again.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24129324
Above indexes cannot be used to optimize your queries because their index expressions are too complex. I would say even the simple presence of MDX file is problematic for ODBC driver.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem: You created a new custom form in Outlook for your contacts (added fields, deleted fields, changed the layout of fields, whatever) and made it the default form for contacts. The good news is that all new contacts will utilize the new form. T…
The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question