VBA how to run a string as a line of code - string

Is there a way to convert a string into an executable line of code?
something like:
Dim Line1 as String
Line1 = "MsgBox (""Hello"")"
Execute Line1
resulting in the pop up box saying Hello.
What I'm doing is pulling lines out of a text file. I can pull lines that are the names of form controls and use them to perform actions on those form controls but I'd like to go one step further and execute lines.
I've seen claims that this would work:
Application.Run Line1
or make the variable an array and store it in element 1 e.g. and use
Application.Run Line1(1)
but it doesn't work for me.
Ok, while writing this I've also been experimenting. I found that
Eval (Line1)
will work when Line1 is a message box, but not when it is something like:
line1 = "DoCmd.OpenForm ""form1"""
Any tips would be appreciated.
Thanks

You can use,
Eval("DoCmd.OpenForm(""form1"")")
You have to make sure any functions you include use parentheses.
Further reference,
http://msdn.microsoft.com/en-us/library/office/aa172212(v=office.11).aspx

It's not exactly what I was asking, I ended up going a slightly different direction, but here's what I ended up doing and it would probably be easily altered to more closely match my question. I actually took lines of text from an external text file and inserted them into a line of code. What I was doing in this example was just hiding columns, the external text file was a list of column names. (figuring out how to output that was fun too)
Open "C:\UserList.txt" For Input As #TextFile
While Not EOF(TextFile)
Line Input #TextFile, TextLine
Screen.ActiveDatasheet.Controls(TextLine).ColumnHidden = True
Wend

Visual Basic is a compiler language, and as such does not support the ability to execute human-readable code while it is running. All code written in VBA must first be compiled BEFORE the program runs the first time.
However, SQL is an interpreter language, and can be handed code which it will execute line by line. You can also fetch contents for variables from other sources while the program runs, you just can't build a string while the program is running and then execute it in VBA directly.

Related

VBA string comparison failure

i met interesting issue when im comparing two strings. Im reading data from file and everything works well. But then co-worker send me input file, which is just CTRL+C and CTRL+V of working file. And then miracle happend! VBA is so confused, that cant compare two simple strings and i fell of chair.
If you take a look at image you can see that comparison passed if condition where are two same strings, but it should not. Im a bit confused how this can happen.
So met someone something like this? Im realy start thinking about something like machine revolution from Terminator. (files are both saved in notepad++ and there are no strange characters or something like that)
Progress update
So i tried hints from guys in comments below. and ended with something like this
If CStr(Trim(rowArray(4))) <> (CStr("N/A")) Then
Contentent of rowArray(4) is still "N/A" string as on picture above and excel still thinks this strings arent same. I also saved file in pspad, netbeans, and normal notepad and issue is still same.
Use the immediate window to test the contents of the variable:
For i = 1 To Len(rowArray(4)): Print Asc(Mid(rowArray(4), i, 1)): Next
This will print the ASCII value of each character in the string - you can use this to determine what the extra character(s) are causing the issue.

Taking numbers from csv file using labviiew

My program should take all numbers from CSV file put them to array and have some problems.
Have can I take some char of string? Here my program:
https://drive.google.com/file/d/0B7eFfQuRzPgAX3o3WmJIb2dzMFU/view?usp=sharing
Example of csv fie:
1, 2, 3, 4
6, 1, 2, 10
Your code is overly complicated. You can load the file using the Read from Spreadsheet file function and simply create a 2D array of the output.
The Read from Spreadsheet function is polymorphic. That means you can select what data type it uses. Right-click on the node and select 'Visible Items' and then 'Polymorphic VI Selector'.
Change the pull-down on the bottom of the node to String, and then create a string constant and set it's value to , (comma).
please consider using deliminator input on read from spreadsheet VI.
Please check attached
The read from spreadsheet file vi is the simplest solution as answered previously. One thing to note about using that function though is that it internally uses the labview error handler with the "Stop" or "Continue" dialog box popup. I've run into the problem where the user cancels out of the file dialog, then sees the error handler dialog box, presses "Stop" and gets confused why the program behaves unexpectedly afterwards.
To prevent this, test the file path using the "File/Directory Info" vi and "Check if File or Folder exists.vi". Put the Read from spreadsheet file in case structure which only runs when the path is not a directory and exists. I tried to attach a snippet to show this, but do not have enough points.

restructuredText inline literal that can wrap to the next line (for PDF output)

I am using restructuredText to create a report which includes tom log file outputs.
What I have is a number of sections with numbered lists of literals.
This looks like this:
#. ``some log file output``
#. ``more output``
Now the problem with this is that when I convert to a PDF from this using rst2pdf, the literals can sometimes be quite long and flow off the page.
What I would love is away to mark a section of text as a code literal that can flow onto the next line just like regular text.
I want this because if I don't mark the log file output as being a literal, there is sometimes some crud within the log file output which rst is interpreting as inline markup or other rst related commands.
Any other suggestions as to how this can be best done?
I know that I could ensure that the source rst file only has lines of a certain width but this would make the source file look horrible and make it unwieldy to edit.
I have tried the following 2 things, both of which don't help:
I found a rst2pdf option:
--fit-literal-mode=MODE
What to do when a literal is too wide.
One of error,overflow,shrink,truncate.
Default="shrink"
After some researching, I found mention of a wrapping option for literals.
I got rst2pdf to dump out the default stylesheet using:
rst2pdf --print-stylesheet which I then saved and modified such that the wordWrap option under literal was changed to CJK.

Read a text file to a string using fortran77

Is it possible to read a text file to a string using fortran77.
I actually have a text file in the following format
Some comments
Some comments
n1 m1 comment_with_unknown_number_of_words
..m1 lines of data..
n2 m2 comment_with_unknown_number_of_words
..m2 lines of data..
and so on
whereas n1,n2.. are the orders of the objects. m1, m2,..are the number of lines which contains the data about these objects, respectively. I also want to store the comment of each object for further investigations.
How can I deal with this? Thank you so much in advance!
I can't believe nobody called me on this.. My apologies this in fact only grabs the first word of the comment...
------------original answer----
Not to recomend F77, but this isnt that tough a problem either. Just declare a char variable long enough to hold your longest comment and use a list directed read.
integer m1,n1
char*80 comment
...
read(unit,*)m1,n1,comment
If you want to write it back out without padding a bunch of extra spaces thats a bit of effort but hardly the end of the world.
What you can not do at all in f77 is discern whether your file has trailing blanks at the end of a line, unless you go to direct access reading.
------------improved answer
What you need to do is read the whole line as a string then read your integers from the string:
read(unit,'(a)')comment
read(comment,*)m1,n1
at this point comment contains the whole line including your two integers (perhaps that will do the job for you). If you want to pull off the actual string it requires a bit of coding (I have a ~40 line subroutine to split the string into words). I could post if interesed but I'm more inclined as others to encourage you to see if your code will work with a more modern compiler.

Fortran 90 - I/O passing variable as filepath

This seems like it should be an easy fix, but after searching for hours I cannot find a solution.
I want to save a filepath as a character variable, then use the variable in a I/O statement. Below is my code for reading from the file defined by "filepath".
character:: filepath
filepath='c:\users\chris\...\data.txt'
open(unit=1,file=filepath,status='old',action='read',form='formatted',iostat=ierror)
if(ierror.NE.0)then
print*,'file cannot be opened or does not exist'
read*,
endif
......
The program compiles (gfortran compiler using NetBeans IDE), but when I run, I get the error printed to the screen.
Note 1: When I put the filepath directly into the open function as shown below, it seems to work fine
open(unit=1,file='c:\\Users\\Chris\\...\\data.txt',status='old',action='read',form='formatted',iostat=ierror)
Note 2: I have tried all variations of forward/backward, single/double slashes in the filepath variable, so I don't think that is the problem. When I print out "filepath" to the screen as a debugging mechanism (before the open function above), all is get back is "C". So for some reason, it seems I am losing the rest of the filepath. (I will leave my questions regarding what slash method is appropriate until I research it more)
I appreciate any suggestions.
Thanks,
Chris
You've declared filepath to be a character variable of length 1. Change the declaration to something like character(80):: filepath (use a length long enough to contain the entire path).

Resources