Discord in Program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Discord in Program

Hybrid View

  1. #1
    N. Richards Guest

    Discord in Program


    My data base has two records)
    "D F# A#"
    "D Aug"
    "D F# A"
    "D Maj"
    When the program below looks for D F# A,it should return the
    second record, but it returns the first because it finds the
    string D F# A in it. This is not what I want it to do. I
    can't sort 12,000 records, so I need a better solution.

    Program:

    'Force define all variables
    Option Explicit
    'Define Type to hold all data
    Type myType
    Entry As String
    Chord As String
    Root As String
    Third As String
    Fifth As String
    Other As String
    End Type
    'Define a constant to indicate maximum entries
    Const maxEntries = 5000
    Const theFilename = "Chordlup.dat"
    'Declare array to hold data
    Dim theData(maxEntries) As myType
    'Declare other variables
    Dim numEntries As Integer, displayedEntry As Integer
    '
    'Initialize the procedure
    '
    '
    'Search for an entry and display it
    '
    Sub FindIt()
    numEntries = -1 'Initialize numentries
    Dim I As Integer
    'Look for data file, if there, open it. If not, initialize it.
    If Dir(theFilename) <> "" Then
    Open theFilename For Input As #1
    Input #1, numEntries
    For I = 0 To numEntries
    Input #1, theData(I).Entry
    Input #1, theData(I).Chord
    Input #1, theData(I).Root
    Input #1, theData(I).Third
    Input #1, theData(I).Fifth
    Input #1, theData(I).Other
    Next I
    Close #1
    Else
    MsgBox ("No data file found")
    numEntries = -1 'initialize numentries
    End If
    For I = 0 To numEntries
    If InStr(1, theData(I).Entry, ActiveSheet.Range("Entry").Value, 1) > 0 Then
    Exit For
    End If
    Next I
    If I = numEntries + 1 Then
    MsgBox ("Not Found")
    Else
    DisplayIt I
    End If
    End Sub

    Sub DisplayIt(theEntry As Integer)
    With ActiveSheet
    .Range("Entry").Value = theData(theEntry).Entry
    .Range("Chord").Value = theData(theEntry).Chord
    .Range("Root").Value = theData(theEntry).Root
    .Range("Third").Value = theData(theEntry).Third
    .Range("Fifth").Value = theData(theEntry).Fifth
    .Range("Other").Value = theData(theEntry).Other
    End With
    End Sub

    Sub AddName()
    numEntries = -1 'Initialize numentries
    Dim I As Integer
    'Look for data file, if there, open it. If not, initialize it.
    If Dir(theFilename) <> "" Then
    Open theFilename For Input As #1
    Input #1, numEntries
    For I = 0 To numEntries
    Input #1, theData(I).Entry
    Input #1, theData(I).Chord
    Input #1, theData(I).Root
    Input #1, theData(I).Third
    Input #1, theData(I).Fifth
    Input #1, theData(I).Other
    Next I
    Close #1
    Else
    MsgBox ("No data file found")
    numEntries = -1 'initialize numentries
    End If
    '
    numEntries = numEntries + 1 'Increment the entry number
    If numEntries > maxEntries Then
    MsgBox ("Too many entries. To enter more, increase maxEntries in Module.")
    numEntries = numEntries - 1
    Exit Sub
    End If
    'Copy the data from the worksheet to the array.
    theData(numEntries).Entry = ActiveSheet.Range("Entry").Value
    theData(numEntries).Chord = ActiveSheet.Range("Chord").Value
    theData(numEntries).Root = ActiveSheet.Range("Root").Value
    theData(numEntries).Third = ActiveSheet.Range("Third").Value
    theData(numEntries).Fifth = ActiveSheet.Range("Fifth").Value
    theData(numEntries).Other = ActiveSheet.Range("Other").Value
    displayedEntry = numEntries
    'Save the new entry
    Open theFilename For Output As #1
    Write #1, numEntries
    For I = 0 To numEntries
    Write #1, theData(I).Entry
    Write #1, theData(I).Chord
    Write #1, theData(I).Root
    Write #1, theData(I).Third
    Write #1, theData(I).Fifth
    Write #1, theData(I).Other
    Next I
    Close #1
    End Sub



  2. #2
    Paul Clapham Guest

    Re: Discord in Program

    You used InStr to compare your strings, and that function finds whether one
    string is a substring of another. Hence the results you see. If you want
    to compare two strings for equality, don't use InStr, use =.

    N. Richards <ndrich@erols.com> wrote in message
    news:39b9a1ac$1@news.devx.com...
    >
    > My data base has two records)
    > "D F# A#"
    > "D Aug"
    > "D F# A"
    > "D Maj"
    > When the program below looks for D F# A,it should return the
    > second record, but it returns the first because it finds the
    > string D F# A in it. This is not what I want it to do. I
    > can't sort 12,000 records, so I need a better solution.
    >





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center