Results 1 to 8 of 8

Thread: Writing Usernames | CSV File | Issue

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2016
    Posts
    34

    Question Writing Usernames | CSV File | Issue

    Hello everyone,

    Currently facing an issue that I have with writing an Array of Records to a CSV file. The code for writing the information is shown below:

    Name:  The Code.jpg
Views: 176
Size:  11.5 KB

    This is the code.

    Name:  Expected.png
Views: 173
Size:  2.4 KB

    This is the expected output when writing to the file.

    Name:  Actual.png
Views: 164
Size:  2.2 KB

    This is the actual outcome.

    Why are the other 2 users being removed from the file?

    Thanks,

    Snowy

  2. #2
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: Writing Usernames | CSV File | Issue

    If you don't type in a user name it will result in a null field. It looks like only user C was actually entered and the other 3 (not 2) nothing was entered

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Writing Usernames | CSV File | Issue

    You should paste your actual code into your post, not screen shots.

    Your code is only asking for one user and has no values for anything else

    Index is defined in the sub and never assigned a value so index=0 which means you loop is from 0 to 0 and will execute only once
    The code also opens the file for output so I would expect that what you are really getting is a single row of data with just whatever was typed in the input box and not what you showed above.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Writing Usernames | CSV File | Issue

    The redim ... when you ReDim an array, it wipes out anything already in the array... if you want to keep data in the array, then you have to use the preserve keyword too:
    ReDim Preserve myArray(newSize)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: Writing Usernames | CSV File | Issue

    Quote Originally Posted by DataMiser View Post
    You should paste your actual code into your post, not screen shots.

    Your code is only asking for one user and has no values for anything else

    Index is defined in the sub and never assigned a value so index=0 which means you loop is from 0 to 0 and will execute only once
    The code also opens the file for output so I would expect that what you are really getting is a single row of data with just whatever was typed in the input box and not what you showed above.
    Index = NumUser and NumUser is probably defined as Public in a .BAS mod and is incremented by 1 each time in the sub so Index will alway have the same value as the number of times into the sub.

    His User table is redimed each time so it clears out previous entries so I suspect he entered the sub 4 times so he only picked up the last entry (#4) from the user table and wrote that out and the first 3 were not in the table because of the redim

    Solution: See post #4
    Last edited by I Love VB6; Mar 13th, 2017 at 09:07 PM.

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Writing Usernames | CSV File | Issue

    Yep I missed that part, hard for these old eyes to read text when it is posted as a low quality image rather than text

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Writing Usernames | CSV File | Issue

    if you just want to add an additional user to the existing file, just append the one new user
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: Writing Usernames | CSV File | Issue

    One further note: You should change your For loop to:
    For Index = 1 To Index
    or move your
    NumUser = NumUser + 1 line
    after you write the file otherwise you get an extra line in your output file

    As it is: For Index = 0 To Index

    "",0,0,0
    "A",0,0,0
    "B",0,0,0
    "C",0,0,0
    "D",0,0,0


    Line changed to For Index = 1 To Index

    "A",0,0,0
    "B",0,0,0
    "C",0,0,0
    "D",0,0,0

    However, post #7 has the best way to go about this and you could simplify your code like this:

    Code:
      '
      '
     NumUser = NumUser + 1
     
     ReDim Preserve User(0 To NumUser) As UserInfo
     
     User(NumUser).UserName = InputBox("Hello: Please enter a username")
     
     Open PlayerFile For Append As #FF
     
     Write #FF, User(NumUser).UserName, User(NumUser).NumCompletedE, User(NumUser).NumCompletedM, User(NumUser).NumCompletedH
    
     Close #FF
     
     Module1.Extract NumUser
      '
      '
    Last edited by I Love VB6; Mar 14th, 2017 at 01:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width