7 Replies - 9073 Views - Last Post: 16 November 2011 - 01:50 PM Rate Topic: -----

#1 risqiaris77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-November 11

runtime error: 9 "subscript out of range"

Posted 10 November 2011 - 06:27 PM

me and my friends make the drug information service application and I got a part in the right form aksess actually been able to run but when I copy it to another laptop suddenly appeared "run time 9: subscript out of range" error that appears when I type username in txtusername.text.
does anyone know why?
This coding that I use in txtusername::

Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Private Sub txtusername_Change ()
If rs.State = adStateOpen Then rs.Close: Set rs = Nothing
     rs.Open "SELECT username FROM operators WHERE username = '" & txtusername.Text & "'", conn, adOpenStatic, adLockOptimistic
     If Not rs.EOF Then
         txtusername.Text = rs ("username")
         call appears
         Label5.Visible = True
         txtpassword.Text = ""
         txtkonfirmasipassword.Text = ""
     else
         Label5.Visible = False
     end If
         If Label5.Visible = False Then
                 txtpassword.Enabled = True
                 txtkonfirmasipassword.Enabled = True
         else
                 txtpassword.Enabled = False
                 txtkonfirmasipassword.Enabled = False
         end If
     Call perform 'call table is first displayed when Form_Load
end Sub


all the help so I hope and impressive..

This post has been edited by AdamSpeight2008: 13 November 2011 - 12:39 PM
Reason for edit:: Please use the CODE TAGs


Is This A Good Question/Topic? 0
  • +

Replies To: runtime error: 9 "subscript out of range"

#2 guyfromri   User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 836
  • Joined: 16-September 09

Re: runtime error: 9 "subscript out of range"

Posted 10 November 2011 - 08:25 PM

I've had this problem in the past too...

I can't tell you why it happens..some machines view the code right and others are slightly different in how the interpret.

But in this case, fairly simple...

Change that line to this
TxtUserName.text=rs.fields("UserName")
'or
TxtUserName.text=rs.fields("UserName").Value
'or
TxtUserName.text=rs!UserName



Any of the 3 should work :)
Was This Post Helpful? 0
  • +
  • -

#3 risqiaris77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-November 11

Re: runtime error: 9 "subscript out of range"

Posted 11 November 2011 - 09:17 PM

View Postguyfromri, on 10 November 2011 - 08:25 PM, said:

I've had this problem in the past too...

I can't tell you why it happens..some machines view the code right and others are slightly different in how the interpret.

But in this case, fairly simple...

Change that line to this
TxtUserName.text=rs.fields("UserName")
'or
TxtUserName.text=rs.fields("UserName").Value
'or
TxtUserName.text=rs!UserName



Any of the 3 should work :)


That way I have tried but it still remains as before,,

is there any other ways and solutions
Was This Post Helpful? 0
  • +
  • -

#4 guyfromri   User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 836
  • Joined: 16-September 09

Re: runtime error: 9 "subscript out of range"

Posted 12 November 2011 - 09:40 AM

If you run each of those pieces, what errors do you get? The same each time or diff? Those are pretty bullet proof ways of programming so if they're returning errors it's possible that it's coming from somewhere else in the script...

Help me help you :)
Was This Post Helpful? 0
  • +
  • -

#5 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: runtime error: 9 "subscript out of range"

Posted 15 November 2011 - 01:59 AM

in which line did you get that error
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: runtime error: 9 "subscript out of range"

Posted 15 November 2011 - 09:21 AM

View Postguyfromri, on 11 November 2011 - 04:25 AM, said:

I've had this problem in the past too...

I can't tell you why it happens..some machines view the code right and others are slightly different in how the interpret.

But in this case, fairly simple...

Change that line to this
TxtUserName.text=rs.fields("UserName")
'or
TxtUserName.text=rs.fields("UserName").Value
'or
TxtUserName.text=rs!UserName



Any of the 3 should work :)

rs("UserName") will work too, if he doesn't put a space between them. :)
Was This Post Helpful? 0
  • +
  • -

#7 raziel_   User is offline

  • Like a lollipop
  • member icon

Reputation: 469
  • View blog
  • Posts: 4,280
  • Joined: 25-March 09

Re: runtime error: 9 "subscript out of range"

Posted 16 November 2011 - 07:33 AM

to be real here the whole code is kinda messed up. in the query to the DB which is rs.Open "SELECT username FROM operators WHERE username = '" & txtusername.Text & "'"... you try to select the username with the input from the textbox txtusrname. however in the line below you check if the result that the query return is equal to the input in the same textbox. If you use the input from the textbox to get the result then it will be equal to the result in the textbox or it will not return nothing. If you want to make the check more descent then you should try using Count() operator in your query:
rs.Open "SELECT Count(ID) FROM operators WHERE username = '" & txtusername.Text & "'", conn, adOpenStatic, adLockOptimistic
If Not rs.EOF Then
If rs(0)>0 Then
'Here the count will return the number of the IDs with this user name'
'It should be 1 if your database design and program logic is correct'
'And it will be 0 if there is no such username with this name'
End if
End If



Also error 9 means that your array is out of range. to be clear this means that rs ("username") is nothing(only a guess since i dont know at which line is the error).

This post has been edited by NoBrain: 16 November 2011 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: runtime error: 9 "subscript out of range"

Posted 16 November 2011 - 01:50 PM

To add onto NoBrain's points, on line 4 when you set rs to nothing, it has precisely no effect whatsoever no matter what the context. Perhaps you can reason out why.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1