Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > PC based Database Applications > Microsoft Excel > How to display text from .txt file or Access through message box?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-24-08, 03:41
blackyroses blackyroses is offline
Registered User
 
Join Date: Sep 2008
Posts: 7
How to display text from .txt file or Access through message box?

Hi all.

I am using Excel VBA 2007 to create a message box, and I'm new to VBA programming But I have managed to create a simple message box with those Excel VBA books that i have borrowed. However, I need to further improve on the codes!

The eg of simple codes:

Code:
Sub ShowMessage() MsgBox ("ARFCN : 79 (E-GSM)" _ & vbCr & "L2 Pseudo Length : 18" _ & vbCr & "Skip indicator : 0" _ & vbCr & "Protocol discriminator : (6) Radio resources management messages" _ & vbCr & "Message type : 27") End Sub

Say the user needs to double-click on a certain cell in the Excel worksheet, after which a message box will popped out to show necessary details for that particular cell. So, how do I display a lump sum of text from a .txt file or from database Access in a message box after double-clicking a cell? Anybody has got any idea?

Last edited by blackyroses : 09-25-08 at 03:37.
Reply With Quote
  #2 (permalink)  
Old 09-24-08, 04:22
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,131
You have to read the text file... Have a look at the FileSystemObject and/or TextStream in help files.

Here's something from my bookmarks that will be useful; http://www.xtremevbtalk.com/showthread.php?t=123814
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #3 (permalink)  
Old 09-24-08, 04:42
blackyroses blackyroses is offline
Registered User
 
Join Date: Sep 2008
Posts: 7
Hi George, thanks for the link! I will take a look at it
Reply With Quote
  #4 (permalink)  
Old 09-24-08, 05:07
blackyroses blackyroses is offline
Registered User
 
Join Date: Sep 2008
Posts: 7
Hi George, to be honest, it's more than a little over my head as I still don't understand. But if this is the only approach, then slogging through it will be a good learning experience for me I'm sure

Thanks again!
Reply With Quote
  #5 (permalink)  
Old 09-24-08, 05:09
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,131
Give it a shot, if you can't get it to work post back your efforts and we'll work on it
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #6 (permalink)  
Old 09-24-08, 05:28
blackyroses blackyroses is offline
Registered User
 
Join Date: Sep 2008
Posts: 7
Thanks ALOT for the help, appreciate it!

Here are the codes:
__________________________________________________ _______________
Sub ShowMessage()

MsgBox ("ARFCN : 79 (E-GSM)" _
& vbCr & "L2 Pseudo Length : 18" _
& vbCr & "Skip indicator : 0" _
& vbCr & "Protocol discriminator : (6) Radio resources management messages" _
& vbCr & "Message type : 27")
End Sub
__________________________________________________ _______________
Sub Open_One_Or_Many_Files()
Dim vaFiles As Variant
Dim i As Long

vaFiles = Application.GetOpenFilename _
(FileFilter:="Text files (*.txt),*.txt", _
Title:="Open File(s)", MultiSelect:=True)

If Not IsArray(vaFiles) Then Exit Sub

With Application
.ScreenUpdating = False
For i = 1 To UBound(vaFiles)
Workbooks.Open vaFiles(i)
Next i
.ScreenUpdating = True
End With

End Sub
__________________________________________________ _______________

The 2nd part, it allows me to open whichever text file that I would like to open, but how can edit the codes to let it open in a messagebox rather than a new Excel workbook?

And also, running the 2 parts together, my original messagebox is gone haha. Why is it so?

Sorry if it's too long or draggy, I'm new to Excel VBA
Reply With Quote
  #7 (permalink)  
Old 09-24-08, 08:57
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,131
Can't believe I forgot this: http://www.dbforums.com/showpost.php...8&postcount=35
This utilises the FileSystemObject to grab a file, like your Application.GetOpenFilename does, however using the FSO you can limit it to only allowing a single selected file.

Anyhow, to modify your code to read the text files first line try this
Code:
vaFiles = Application.GetOpenFilename _ (FileFilter:="Text files (*.txt),*.txt", _ Title:="Open File(s)", MultiSelect:=True) If Not IsArray(vaFiles) Then Exit Sub Dim fs As Object Dim filePath As String Dim a As Variant Const ForAppending As Integer = 8 'Grab the first file from the array filePath = vaFiles(0) 'MsgBox filePath Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.OpenTextFile(filePath, ForAppending) 'Display first line MsgBox a.ReadLine a.Close Set a = Nothing Set fs = Nothing
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #8 (permalink)  
Old 09-24-08, 23:00
blackyroses blackyroses is offline
Registered User
 
Join Date: Sep 2008
Posts: 7
Hi George, sorry for the late reply and thanks again!

I opened the FSO Access database, but I couldn't view the codes. And anyway, I have tried the codes that you mentioned:

__________________________________________________ ___________
Sub ShowMessage()

MsgBox ("ARFCN : 79 (E-GSM)" _
& vbCr & "L2 Pseudo Length : 18" _
& vbCr & "Skip indicator : 0" _
& vbCr & "Protocol discriminator : (6) Radio resources management messages" _
& vbCr & "Message type : 27")
End Sub
__________________________________________________ ___________
Sub Open_One_Or_Many_Files()

Dim vaFiles As Variant

vaFiles = Application.GetOpenFilename _
(FileFilter:="Text files (*.txt),*.txt", _
Title:="Open File(s)", MultiSelect:=True)

If Not IsArray(vaFiles) Then Exit Sub

Dim fs As Object
Dim filePath As String
Dim a As Variant
Const ForAppending As Integer = 8

'Grab the first file from the array
filePath = vaFiles(0)
'MsgBox filePath

Set fs = CreateObject("Scripting.FileSystemObject")

Set a = fs.OpenTextFile(filePath, ForAppending)

'Display first line
MsgBox a.ReadLine

a.Close
Set a = Nothing
Set fs = Nothing
End Sub
__________________________________________________ ___________

However, this appears:

Run-time error '9':
Subscript out of range

I have highlighted the error part in red. Why is it so?
Reply With Quote
  #9 (permalink)  
Old 09-25-08, 04:22
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,131
Try vaFiles(1) instead?
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #10 (permalink)  
Old 09-25-08, 22:12
blackyroses blackyroses is offline
Registered User
 
Join Date: Sep 2008
Posts: 7
I have tried, and there is a new error:
Run-time error '54':
Bad file mode.

Code:
Sub Open_One_Or_Many_Files() Dim vaFiles As Variant vaFiles = Application.GetOpenFilename _ (FileFilter:="Text files (*.txt),*.txt", _ Title:="Open File(s)", MultiSelect:=True) If Not IsArray(vaFiles) Then Exit Sub Dim fs As Object Dim filePath As String Dim a As Variant Const ForAppending As Integer = 8 'Grab the first file from the array filePath = vaFiles(1) 'MsgBox filePath Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.OpenTextFile(filePath, ForAppending) 'Display first line MsgBox a.ReadLine a.Close Set a = Nothing Set fs = Nothing End Sub

Very sorry to trouble you again George
Reply With Quote
  #11 (permalink)  
Old 09-26-08, 04:27
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,131
Oh dear, spotted the mistake...
Code:
Set a = fs.OpenTextFile(filePath, ForAppending) MsgBox a.ReadLine
We need to open the file ForReading!
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #12 (permalink)  
Old 09-29-08, 05:08
blackyroses blackyroses is offline
Registered User
 
Join Date: Sep 2008
Posts: 7
Hi georgev, thanks for the help!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On